Computers are playing an increasing role in education. Write a program that will help an elementary-school student learn multiplication. Use Math.random to produce two positive one-digit integers. It should then display a question such as How much is 6 times 7? The student then types the answer into a text field. Your program checks the student’s answer. If it is correct, display the string Very Good! and generate a new question. If the answer is wrong, display the string No. Please try again. and let the student try the same question again repeatedly until the student finally gets it right. A separate function should be used to generate each new question. This function should be called once when the script begins execution and each time the user answers the question correctly.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<!-- JavaScript by Eduardo Lauro P. Abad -->
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>Web 2 HW: ch 9.27 pg 360</title>
<script type="text/javascript">
var number1 = getnumber();
var number2 = getnumber();
var answer = getanswer(number1,number2);
document.write("How much is " + number1 + " times " + number2 + "? <br/>");
function getnumber()
{
var minn = 1;
var maxn = 9;
number = minn + Math.floor(Math.random()*maxn);
return number;
}
function getanswer(a,b)
{
answer = a*b;
return answer;
}
function testResults (form)
{
var useranswer = form.inputbox.value;
if(useranswer == answer)
{
window.alert("Your answer is " + useranswer + ". The right answer is " + answer + ". Very Good!");
window.location.reload();
}
else
{
window.alert("No. Please try again.");
}
}
</script>
</head>
<body>
<p>
<form action="">
<input type="text" name="inputbox" value="">
<input type="button" name="button" value="Click" onClick="testResults(this.form)">
</form>
</p>
<p><a href="javascript: window.location.reload()">Click here to reload the page.</a></p>
</body>
</html>
Please note that this is in no way the best possible way of doing it. I’m only posting this in my blog so that they are not lost in the future unless something happens to my dedicated server. Plus it is a good way to look back at how you used to write code.