Im trying to figure out how to make the Math.random stop. I have a confirm statement if the the user gets a write answer, it'll alert and ask to confirm. however, math random keeps asking a new question instead of stopping the script when the user clicks "cancel",also if the answer is wrong it shouldn't ask a new question unless the user gets it right. How do I correct this? (sorry Im a newbie, and thanks!)
<h1>Math Random</h1>
<hr>
<br>
<p id="question"></p>
<script>
var x = Math.round(Math.random() * 10);
var y = Math.round(Math.random() * 10);
var outHTML = "How much is " + x + " times " + y + "?";
document.getElementById("question").innerHTML = outHTML;
function getAnswer() {
if (parseInt(document.getElementById("answer").value) == x * y) {
alert("Very Good!");
confirm("Want more?");
} else {
alert("Try again!");
}
}
</script>
<form>
<input id="answer" type="number" placeholder="answer here">
<button onclick="getAnswer()">Submit</button>
</form>
Aucun commentaire:
Enregistrer un commentaire