mercredi 11 mars 2020

How to create random math questions with Javascript

What do I need to add to the code, so that the numbers are between 1 and 9 (without decimals) and are generated randomly and are not static like currently. I also don't know how to make the + - * and / random. What do I need to add to the code, so that it generates a random math question with random opperators and still calculates in the answer section the right answer. I would be really grateful for your advice, help and time.
I have been trying for hours and I’m still failing. So here’s the code with static numbers:

<!DOCTYPE html>
<html>
<body>

<p>Calculate: (1 · 2) + (1 + 2 + 3 – 4) – (3 · 4)</p>

<input id="answer by User">

<button onclick="F1()"> next </button>

<p id="answer"></p>

<script>
function F1() 

{
var x, text;

x = document.getElementById("answer by User").value;

if (isNaN(x) || x < -8 || x > -8) 
{ text = "wrong"; } 

else 
{ text = "correct";}

document.getElementById("answer").innerHTML = text;
}
</script>

<button onclick="F2()"> <b>answer</b> </button>

<div id="step1DIV" style="display: none;"> <p> -8 </p> </div>

<script>
function F2()
{
var x = document.getElementById("step1DIV");
if (x.style.display === "none") {
x.style.display = "block";
} else {
x.style.display = "none";
}
}
</script>

<button onclick="F3()"> <b>Step 1</b> </button>

<div id="S1DIV" class="hidden">
<p> <div id="step1DIV"> <p>multiply <b style="color:red;">(1 · 2)</b> and <b style="color:red;">(3 · 4) </b> </p>

<p>= <b style="color:red;">(1 · 2)</b> + (1 + 2 + 3 – 4) – <b style="color:red;">(3 · 4)</b> </p> 

<p> = <b style="color:red;">2</b> + (1 + 2 + 3 – 4) – <b style="color:red;">12</b> </p>
</div> </p> </div>

<style>
.hidden {
display: none;
}
</style>

<script>
function F3()
{
var x = document.getElementById("S1DIV");
x.classList.toggle("hidden");
}
</script>

</body>
</html>



Aucun commentaire:

Enregistrer un commentaire