B
Bender
I am trying to learn javascript using a book, and all was going ok
until I got to this problem, and there is no help offered for this
problem in the book.
I am suppose to build a program that gives a total of 5 random math
problems, but each one repeats until the user gives the right answer.
Only move onto the next question after the previous one was answered
correctly. It is suggested that a for and while loop be used. In
addition to what you see below in my code, there have been references
made to break and switch in earlier chapters.
If someone could get me on the right track and provide a solution,
that would be great! Thanks in advance for your help.
<html>
<head>
<title>Add Two Random Numbers</title>
<script>
var num1;
var num2;
var correct;
var guess = 0;
var i = 0;
//for (i = 0; i <=4; i++){
while (1 <= 4){
num1 = Math.floor(Math.random() * 100) + 1;
num2 = Math.floor(Math.random() * 100) + 1;
correct = num1 + num2;
guess = eval(prompt("What is "+num1+" + "+num2+"?", guess));
if (guess != correct){
alert("Nope, that's not it.");
guess = eval(prompt("What is "+num1+" + "+num2+"?", guess));
}else{
alert("Wow, you are smart!");
i++;
} // end for
} // end while
//} // end for
</script>
</head>
<body><center><h1>Add two random numbers</h1></center><hr></body>
</html>
until I got to this problem, and there is no help offered for this
problem in the book.
I am suppose to build a program that gives a total of 5 random math
problems, but each one repeats until the user gives the right answer.
Only move onto the next question after the previous one was answered
correctly. It is suggested that a for and while loop be used. In
addition to what you see below in my code, there have been references
made to break and switch in earlier chapters.
If someone could get me on the right track and provide a solution,
that would be great! Thanks in advance for your help.
<html>
<head>
<title>Add Two Random Numbers</title>
<script>
var num1;
var num2;
var correct;
var guess = 0;
var i = 0;
//for (i = 0; i <=4; i++){
while (1 <= 4){
num1 = Math.floor(Math.random() * 100) + 1;
num2 = Math.floor(Math.random() * 100) + 1;
correct = num1 + num2;
guess = eval(prompt("What is "+num1+" + "+num2+"?", guess));
if (guess != correct){
alert("Nope, that's not it.");
guess = eval(prompt("What is "+num1+" + "+num2+"?", guess));
}else{
alert("Wow, you are smart!");
i++;
} // end for
} // end while
//} // end for
</script>
</head>
<body><center><h1>Add two random numbers</h1></center><hr></body>
</html>