Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialAndrea Faciane
10,042 Pointsunable 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
Alexander Davison
65,469 PointsIn 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.
~Alex
egaratkaoroprat
16,630 PointsThe 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!
Andrea Faciane
10,042 Pointsthanks Alex yet changing randomNumber to random resolve the issue. I don't see the results by randomly choosing a number from 1 to 6.
Andrea Faciane
10,042 PointsI'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>');} }
egaratkaoroprat
16,630 Pointsyou accidentally replaced a ' with a . on your last line right before </p>
Andrea Faciane
10,042 PointsI removed the . and there's no difference.
egaratkaoroprat
16,630 PointsPlease 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
1,869 PointsI run your code Egarat but I don't get the document.write to pop up after I guess a random number?
egaratkaoroprat
16,630 PointsIt works for me. Maybe there is a typo?
egaratkaoroprat
16,630 Pointsegaratkaoroprat
16,630 Pointsoops, I was too slow :D