Addition with math.random
Title is self explanatory - I cannot figure out how to get this code working. Any help?
<title> Addition </title>
var guess = -1; // Set guess to a value outside the range of valid values
var num1 = parseInt(Math.random()*10); // get a number between 0 and 9
var num2 = parseInt(Math.random()*10); // get a number between 0 and 9
sum = num1 + num2;
alert("(to help test) The correct answer is " + sum); // Just to help test
// Use a while loop
while (guess != sum)
guess = prompt("Guess a number from 1 to 10", "");
if (guess == sum)
else if (guess < sum)
alert("Guess higher ");
It works. In what way does it fail for you?
Where used, return should be executed unconditionally and always as the last statement in the function.
That's my signature, it's not part of the damn post!
When you pick two numbers between 1 and 10, the user is not then guessing a value between 1 and 10, the sum falls between 2 and 20 (inclusive).
You should throw in some logic that verifies that the user entered a number between 1 and 10. Not critical, but it is odd to see "guess lower" when I enter a letter or special character instead of a number.
That's true. I'll update that
Users Browsing this Thread
There are currently 1 users browsing this thread. (0 members and 1 guests)