Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

JavaScript JavaScript Basics (Retired) Making Decisions with Conditional Statements Build a Random Number Guessing Game

unable to see results

var random = Math.floor(Math.random() * 6 ) + 1;
var guess = prompt ('I am thinking of a number between 1 and 6.What is it?');
if (parseInt(guess) === randomNumber) {
 document.write('<p>You guessed the number!</p>'); 
} else {
  document.write('<p>Sorry. the number was ' + randomNumber + ' </p>');
}

6 Answers

In your program, you are trying to say parseInt(guess) === randomNumber although the randomNumber variable doesn't exist.

You only created two variables: guess and random. To fix your program, you can either change randomNumber to random or you can rename the variable at the beginning (where your defined the variable) to randomNumber.

Either way, both the variable used in the condition has to match up with the variable you made.

I hope this helps. :grin:

~Alex

oops, I was too slow :D

The reason is that you put the generated random number in avariable called 'random'. You might want to change the name to 'randomNumber', so it matches with the rest of your code.

If you are working with Chrome, you can open the Dev Tools (F12 on Windows) and switch to the console tab. There you could see that it says 'randomNumber is not defined', which would help you figure out the problem.

Hope this helps!

thanks Alex yet changing randomNumber to random resolve the issue. I don't see the results by randomly choosing a number from 1 to 6.

I'm not getting a prompt now... please help

var randomNumber = Math.floor(Math.randomNumber() * 6 ) + 1;
var guess = prompt('I am thinking of a number between 1 and 6. What is it?');
if (parseInt(guess) === randomNumber ) {
 document.write('<p>You guessed the number!</p>');
} 
 else {
document.write('<p>Sorry. The number was ' + randomNumber + .</p>');}              }

you accidentally replaced a ' with a . on your last line right before </p>

I removed the . and there's no difference.

Please look at this code and see if you can find any differences (hint: I made 3 changes to the code you posted above):

var randomNumber = Math.floor(Math.random() * 6 ) + 1;
var guess = prompt('I am thinking of a number between 1 and 6. What is it?');
if (parseInt(guess) === randomNumber ) {
 document.write('<p>You guessed the number!</p>');
} 
 else {
document.write('<p>Sorry. The number was ' + randomNumber + '</p>');
} 

Let me know if something's not clear.

Ismael Barba
Ismael Barba
1,869 Points

I run your code Egarat but I don't get the document.write to pop up after I guess a random number?

It works for me. Maybe there is a typo?