Newbie Help Please - I think this is an easy one for you verterans

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>
 
G

George M Jempty

Bender said:
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){

1 will always be <= 4; don't you mean while (i <= 4)?
 
R

Richard Cornford

guess = eval(prompt("What is "+num1+" + "+num2+"?", guess));
<snip>

You are using eval for string to number type conversion. There are 5 or
6 ways of doing string to number type conversion of which eval is the
worst. If the input string is not a number the result of the eval is
unpredictable, for example, if the user types in "correct" they will
pass the test without effort. All of the other methods return NaN if the
input string cannot be converted into a number. The optimum string to
number type-converting method is the use of the unary plus operator:-

var numValue = (+stringValue);

- it is about 40 times faster than - eval - and has well specified
behaviour when the input is not a string representing a number. It is
also worth mentioning that the use of the - eval - function is generally
frowned upon and almost never (if not actually never) necessary in
JavaScript.

<script type="text/javascript">
(function(){
var guess, correct, stData = ["What is ",""," + ","","?"];
for(var c = 5;c--;){
guess = '';
correct = (stData[1] = Math.ceil(Math.random() * 100))+
(stData[3] = Math.ceil(Math.random() * 100));
while(true){
if((guess = +prompt(stData.join(''), guess)) != correct){
alert("Nope, that's not it.");
}else{
//alert("Wow, you are smart!");
break;
}
}
}
})();
</script>

Richard.
 
B

Bender

Thanks for the help and the insight to eval. That is just what I have
been taught so far using the book. I will remember it in the future
tho... thanks.

As I have not learned functions yet, is there a way to solve this
using similar to the coding that I had in the original post? I realize
that it may not be the easiest or best way to code, but it would be
what I have learned so far, and that would have me see it in a way
that I would understand what was going on based upon what I have
learned so far.

Thanks for the help!


Richard Cornford said:
guess = eval(prompt("What is "+num1+" + "+num2+"?", guess));
<snip>

You are using eval for string to number type conversion. There are 5 or
6 ways of doing string to number type conversion of which eval is the
worst. If the input string is not a number the result of the eval is
unpredictable, for example, if the user types in "correct" they will
pass the test without effort. All of the other methods return NaN if the
input string cannot be converted into a number. The optimum string to
number type-converting method is the use of the unary plus operator:-

var numValue = (+stringValue);

- it is about 40 times faster than - eval - and has well specified
behaviour when the input is not a string representing a number. It is
also worth mentioning that the use of the - eval - function is generally
frowned upon and almost never (if not actually never) necessary in
JavaScript.

<script type="text/javascript">
(function(){
var guess, correct, stData = ["What is ",""," + ","","?"];
for(var c = 5;c--;){
guess = '';
correct = (stData[1] = Math.ceil(Math.random() * 100))+
(stData[3] = Math.ceil(Math.random() * 100));
while(true){
if((guess = +prompt(stData.join(''), guess)) != correct){
alert("Nope, that's not it.");
}else{
//alert("Wow, you are smart!");
break;
}
}
}
})();
</script>

Richard.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,021
Latest member
AkilahJaim

Latest Threads

Top