B
Bender
I am attempting to learn JavaScript using a book and lots of trial and
error. I am stuck on a problem that I posted a few days ago and got a
great solution to (with some additional info on eval), but not one
that I understand. I have not yet learned the things that were in that
solution. I need help in a way that I can understand what is being
done so I can learn it.
Here's the problem... I am trying to build a script that asks 5 random
math problems and doesn't let you move on to the next one until the
previous one has been solved.
The hint in the book for this was that it would require a for loop and
a while loop. The book covered break and switch in a previous chapter,
but I don't think it is to be used for this problem. It would be great
if the book offered solutions for all problems, not just some of
them...
This is what I have, but it just ain't right. Any help would be
greatly appreciated... and thanks!!
<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 (i <= 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>
error. I am stuck on a problem that I posted a few days ago and got a
great solution to (with some additional info on eval), but not one
that I understand. I have not yet learned the things that were in that
solution. I need help in a way that I can understand what is being
done so I can learn it.
Here's the problem... I am trying to build a script that asks 5 random
math problems and doesn't let you move on to the next one until the
previous one has been solved.
The hint in the book for this was that it would require a for loop and
a while loop. The book covered break and switch in a previous chapter,
but I don't think it is to be used for this problem. It would be great
if the book offered solutions for all problems, not just some of
them...
This is what I have, but it just ain't right. Any help would be
greatly appreciated... and thanks!!
<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 (i <= 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>