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!
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
Arikaturika Tumojenko
8,897 PointsWhat is wrong with my code? The dialog box types out the wrong message.
I have this code
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 value I am thinking of is lower than " + guess);
if (parseInt(guessMore) === randomNumber ) {
correctGuess =true;}
} else if (parseInt(guess) > randomNumber) {
var guessLess = prompt("Try again! The number I am thinking of is smaller than " + guess);
if (parseInt(guessLess) === randomNumber) {
correctGuess = true; }
}
if ( correctGuess) {
alert("Yey!");
} else {
alert("Better luck next time! The number was " + randomNumber);
};
Although the code seems correct to me, it doesn't work as expected. Sometimes it displays messages like "Better luck next time! The number was 4", although the number I typed was 4. Or it asks for a smaller number, only to find out the result was bigger etc. Please helps.
4 Answers

Philip G
14,600 PointsHi Arikaturika,
Ignore this answer, I overlooked the curly brace :) Your code works fine for me
You're missing a closing curly brace after the last else if. This code should work as expected:
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 value I am thinking of is lower than " + guess);
if (parseInt(guessMore) === randomNumber ) {
correctGuess =true;}
} else if (parseInt(guess) > randomNumber) {
var guessLess = prompt("Try again! The number I am thinking of is smaller than " + guess);
if (parseInt(guessLess) === randomNumber) {
correctGuess = true; }
}
}
if ( correctGuess) {
alert("Yey!");
} else {
alert("Better luck next time! The number was " + randomNumber);
};
Best Regards, Philip

Arikaturika Tumojenko
8,897 PointsI tried your code, and it doesn't work at all. I have 4 curly braces on my last if else statement. So it must be something else :(.

Philip G
14,600 PointsWhen I test the code you posted in my browser, it works fine. Please try to reproduce the issue in another browser

Arikaturika Tumojenko
8,897 PointsI just did, using Firefox. Same issue, the alert box displays illogical results. Have you tried it a bunch of times, to make sure it doesn't give you an error? Most of the time the code works but one of let's say, 5 times, it doesn't.