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 trial
Bill Le
3,958 PointsI guessed the right number but it alerts the last else statement
I went through the program to test it. Everything is going as it should be, but when I do guessed the number right after the second prompt it runs the else statement. example: I guessed 6. 'Try again. I am thinking of a number less than 6' I guessed 4. 'Sorry. The correct number is 4'
<p>
var correctGuess = false;
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 ) {
correctGuess = true;
} else if(parseInt(guess) < randomNumber){
var guessMore = prompt('Try Again. The number I am thinking of is more than ' + guess);
if (parseInt(guess) === randomNumber){
correctGuess = true;
}
} else if(parseInt(guess) > randomNumber){
var guessLess = prompt('Try again. I am thinking of a number that is less than ' + guess);
if (parseInt(guess) === randomNumber){
correctGuess = true;
}
}
//----------------------
if ( correctGuess ) {
document.write('<p>You guessed the number!</p>');
} else {
document.write('<p>Sorry. The number was ' + randomNumber + '.</p>');
}
</p>
1 Answer
Jennifer Nordell
Treehouse TeacherHi there! You've set up variables for guessMore and guessLess. The values assigned to these are returned by input from the user. However, your following if statement is checking the value of guess. But the value of guess has never been changed. It's still what it was the first time the user was asked. You could either check the value of guessMore and guessLess or you could simply reset the value of guess to the user's next guess.
Hope this helps!
Bill Le
3,958 PointsBill Le
3,958 PointsYeah I just realize I'm not checking the value of guessMore and guessLess. Thanks for the help Jennifer!