We can create a math drill function for kids using JavaScript.
In this example, the function generates two integers and add them together. After that, it comparesits result to the answer entered by the user. If the answer is correct, it will show the message ‘Correct’. On the other hand, if the answer is wrong, it will show the message ‘ Incorrect’.
In order to generate a random number, we use the built-in function, as follows:
Math.random()
In order to statement generates an integer that is less than or equal to 100, we use the parseInt function.
parseInt(Math.random()*100);
The following is the JavaScript code created by us:
function myFunction() {
var number1;
var number2;
var answer;
var message
number1=document.getElementById(‘num1’).innerHTML ;
number2=document.getElementById(‘num2’).innerHTML;
answer=document.getElementById(‘ans1’).value;
var sum=parseInt(number1)+parseInt(number2);
if (answer==sum)
{message=”Correct”;}
else
{message=”Incorrect”;}
document.getElementById(‘ans’).textContent=message;
}
Check it out here,