real new newbie help please

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

Dave Griffiths

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>

Hi

I just tieded up the code a little, for beginers like us (yep me too) it's
much easier to read the code with a little space between the code and
correct indenting so you can see where the loops and things start and
finish

<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));

// escape from loop
if (guess == -1) {
break
}

if (guess != correct) {
alert("Nope, that's not it.");
guess = eval(prompt("What is " + num1 + " + " + num2 + "?", guess));
} else {
alert("Wow, you are smart! Loop num " + i);
i++;
}
}

</script>
</head>
<body><center><h1>Add two random numbers</h1></center><hr></body>
</html>
 
B

bender b

Thanks for tidying it up, hopefully now it will be easier for someone to
see a solution!
 
L

Lasse Reichstein Nielsen

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.

That is a good suggestion. You can even use them in that order.

The for loop is typically used to count from a number to another. That
makes it good for counting to five.

A while loop is typically used to repeat something until a condition
holds. That makes it good for repeating until an answer is correct.

(A similar construct, repeat{}while(), would be even better, but I
guess that is yet to come in the book).
The book covered break and switch in a previous chapter,
but I don't think it is to be used for this problem.

Probably not. I can't see a good way to use them.
This is what I have, but it just ain't right. Any help would be
greatly appreciated... and thanks!!

I'll just give you my solution, annotated.

---
var num1, num2, correct, guess, i;

for (i=0; i<=4; i++) { // repeat five times.
num1 = Math.floor(Math.random()*100) + 1;
num2 = Math.floor(Math.random()*100) + 1;
correct = num1 + num2; // agree so far.

var guess = 0; // not correct answer.
while (guess != correct) { // while we haven't found the result:
guess = parseInt(prompt("What is "+num1+" + "+num2+"?",guess),10);
if (guess == correct) { // repond to guess.
alert("Wow, you are smart!");
} else {
alert("Nope, that's not it.");
}
} // end while
} // end for
---

Another version with repeat:

---
var num1, num2, correct, guess, i;

for (i=0; i<=4; i++) { // repeat five times.
num1 = Math.floor(Math.random()*100) + 1;
num2 = Math.floor(Math.random()*100) + 1;
correct = num1 + num2; // agree so far.

repeat { // repeat while we haven't found the result:
guess = parseInt(prompt("What is "+num1+" + "+num2+"?",guess),10);
if (guess != correct) { // respond to wrong guess.
alert("Nope, that's not it.");
}
} while (guess != correct); // end repeat
alert("Wow, you are smart!"); // we got here, so guess is correct
} // end for
 
B

bender b

Excellent! This is just what I needed.. thank you very much.

Can you please tell me why the 10 is in this line: guess =
parseInt(prompt("What is "+num1+" + "+num2+"?",guess),10);

Thanks again!!!
 
L

Lasse Reichstein Nielsen

bender b said:
Excellent! This is just what I needed.. thank you very much.

Can you please tell me why the 10 is in this line: guess =
parseInt(prompt("What is "+num1+" + "+num2+"?",guess),10);

The second argument to parseInt is the number base. You can use it
to parse other bases than decimal, e.g. binary:
parseInt("10101010",2) == 170

If you don't add a second argument, parseInt tries to guess the
base in a very simplistic way: if the string starts with the digit
zero, it assumes the number is in octal, otherwise it assumes
decimal.

To avoid accidental misinterpretations, always use the second
argument on parseInt.

There are *many* other ways to convert a string to a number, and
none of them suffer from the same problem. I used parseInt because
the name is self-explanatory :).

Other methods of converting a string to a number:
parseFloat(string)
Number(string)
(string - 0)
(string * 1)
(string / 1) // i.e., most binary mathematical operations except plus
+string // unary plus works. It corresponds to the prefix minus
// for negative numbers

The last one is, as far as I have understood from people who have
tested it, both the shortest and the fastest, although the difference
between that and *1 is minuscule.

/L
 
F

Fred Serry

Lasse Reichstein Nielsen said:
If you don't add a second argument, parseInt tries to guess the
base in a very simplistic way: if the string starts with the digit
zero, it assumes the number is in octal, otherwise it assumes
decimal.

Small correction:
If the string begins with "0x", the radix is 16 (hexadecimal).
If the string begins with "0", the radix is 8 (octal).
If the string begins with any other value, the radix is 10 (decimal).
Still simplistic though!

:) Fred
 
D

Dr John Stockton

JRS: In article <[email protected]>, seen in
news:comp.lang.javascript said:
A while loop is typically used to repeat something until a condition
holds. That makes it good for repeating until an answer is correct.

(A similar construct, repeat{}while(), would be even better, but I
guess that is yet to come in the book).

In javascript, the word "repeat" is spelt with a 'd' and an 'o', in that
order, only; implemented from JS 1.2; in it, continue errs in NN4.
According to the Small Flanagan book.

It's in ECMA 262, sec 12.6.
 
L

Lasse Reichstein Nielsen

Dr John Stockton said:
In javascript, the word "repeat" is spelt with a 'd' and an 'o', in that
order, only; implemented from JS 1.2; in it, continue errs in NN4.
According to the Small Flanagan book.

Do(h)!

That just goes to show how much I have used that construct in the
fifteen+ years I have used C and similar languages.

/L
 

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,774
Messages
2,569,596
Members
45,142
Latest member
arinsharma
Top