vendredi 18 septembre 2020

How do I output a new random number to an HTML doc(via innerText) in JavaScript?

I am doing a simple math game in HTML and JavaScript for school, this is just a small part of it. Basically I have a button that I want to be able to press, and each time I press it I want it to output a new number to the innerText of a div in the HTML document.

Right now I get the random number, but I have to refresh the page to get a new one since it keeps outputting the same one if I press it again.

const mathQuestion = document.getElementById('math-question')
const newNumbers = document.getElementById('new-btn')

newNumbers.addEventListener('click', setQuestion)

function setQuestion () {

    mathQuestion.innerText = calc.toString();
    console.log(calc);
}

var calc = Math.floor(Math.random() * 100) + 1;

Yes, it's very simple and probably not the easiest or even the correct way to do it, but I am at a loss here.

I suspect I need to run another function for reseting the text or something after ive set the innerText, but I don't really know where to start.




Aucun commentaire:

Enregistrer un commentaire