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

JavaScript JavaScript Basics (Retired) Making Decisions with Conditional Statements Improving the Random Number Guessing Game

code looks correct but will not run

I have checked my code against the video and it looks to be correct, but the program won't fully run when I preview it. I see only "Random Number Guessing Game" and I am not prompted to enter my guess.

There must be an error in my code, but I have not been able to figure out. Is something missing or is the syntax incorrect?

Here is my 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 number I am thinking of is more 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 less than ' + guess); if (parseInt(guessLess) === randomNumber { correctGuess = true; } } if ( correctGuess ) { document.write('<p>You guessed the number!</p>'); } else { document.write('<p>Sorry. The number was ' + randomNumber + '.</p>'); }

1 Answer

Steven Parker
Steven Parker
231,236 Points

Unformatted code is very hard to read! Use the instructions for code formatting in the Markdown Cheatsheet pop-up below the "Add an Answer" area. :arrow_heading_down:   Or watch this video on code formatting.

But I did manage to spot this in it:

if (parseInt(guessLess) === randomNumber   { correctGuess = true; }

Note that there is no closing parenthesis at the end of the conditional expression and before the code block.

Hi Steven,

Thanks for the suggestions for submitting code, which I will follow in the future. Also, the problem you spotted must be the error, as the program is now running correctly.

Many thanks!