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

Anthony C
Anthony C
4,636 Points

prompt is not popping up

I am following the code from the video but my prompts are not working. Not sure why.

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) > randomNumer ) {
  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>');
}

MODERATOR EDIT: Added Markdown to the code snippet so that it is readable in the community. Please refer to the Markdown Cheatsheet when posting code snippets for the proper way to do so in the Community Forum.

Matthew Smith
Matthew Smith
10,945 Points

You didn't close your conditional statement on line 11 -- there's a " { " but not a closer "}".

Here's what I did and made it work!

} else if ( parseInt(guess) > randomNumer ) {
  var guessLess = prompt('Try again. The number I am thinking of is less than ' + guess);
}if (parseInt(guessLess) === randomNumber) {
      correctGuess = true;
}
Anthony C
Anthony C
4,636 Points

Thank you for the help!

I'm having a similar problem.

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>'); }