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 trialCharlotte Junker
10,261 PointsCan't find my error
I just can't work out where I'm going wrong... I think I have the same code as Dave but in Workspaces I get an infinite loop that crashes the browser, and in Atom it writes to the document after one guess - even if the guess is wrong. Any guidance much appreciated. Thanks!
var randomNumber = getRandomNumber(10);
var guess;
var guessCount = 0;
var correctGuess = false;
function getRandomNumber( upper ) {
var num = Math.floor(Math.random() * upper) + 1;
return num;
}
do {
guess = prompt('What number am I thinking of?');
guessCount += 1;
if (parseInt(guess) === randomNumber) {
correctGuess = true;
}
}
while ( correctGuess = false )
document.write('Well done! I was thinking of ' + randomNumber + ' and it took you ' + guessCount + ' to guess it!');
1 Answer
Alexander Davison
65,469 PointsRemember, you use one equal sign for variable assigning, and three equal signs for equality. take a look at this:
var randomNumber = getRandomNumber(10);
var guess;
var guessCount = 0;
var correctGuess = false;
function getRandomNumber( upper ) {
var num = Math.floor(Math.random() * upper) + 1;
return num;
}
do {
guess = prompt('What number am I thinking of?');
guessCount += 1;
if (parseInt(guess) === randomNumber) {
correctGuess = true;
}
}
while ( correctGuess === false ) // Now this is fixed :)
document.write('Well done! I was thinking of ' + randomNumber + ' and it took you ' + guessCount + ' to guess it!');
Also, you can reduce code even more by changing correctGuess === false into !correctGuess. This works because if correctGuess was false, !correctGuess (which is the opposite of correctGuess) would be true. This is the final result:
var randomNumber = getRandomNumber(10);
var guess;
var guessCount = 0;
var correctGuess = false;
function getRandomNumber( upper ) {
var num = Math.floor(Math.random() * upper) + 1;
return num;
}
do {
guess = prompt('What number am I thinking of?');
guessCount += 1;
if (parseInt(guess) === randomNumber) {
correctGuess = true;
}
}
while ( !correctGuess ) // Now THIS looks even better! :D
document.write('Well done! I was thinking of ' + randomNumber + ' and it took you ' + guessCount + ' to guess it!');
Hope this helps! ~Alex
Charlotte Junker
10,261 PointsCharlotte Junker
10,261 PointsThanks Alex! Sorry I took so long to get back to you but that works :)
Alexander Davison
65,469 PointsAlexander Davison
65,469 PointsNo problem :)