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

Jason Brown
Jason Brown
9,626 Points

If the second prompt says "guess less than ... " then the document.write command does not work. I can't figure out why?

All other options in my code work fine, but the above problem renders a blank space where it should either read: "You guessed the number!" or "Sorry. The number was ..." here's 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>'); } '''

2 Answers

Doegena Fennich
Doegena Fennich
8,974 Points

To see the result you should change if( parseInt(guess) === randomNumber ) to if(correctGuess)

Like this;

function randomGame() {

  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 ) { 
          alert("Correct answer"); } else {
          alert("sorry the right answer was " + randomNumber)
          }
}          

randomGame();
Jason Brown
Jason Brown
9,626 Points

Thank you so much Doegena! In the video he keeps two else if statements and I wanted to be sure I could replicate exactly what Dave was doing. The code you provided helped me hone in on where the problem was. I found out why it wasn't working ... my "var guessLess" was initially named "guessless". I spent waaaay too long looking at this thing to figure it out. Beginner JS rookie mistake.

Thanks again!

'

Doegena Fennich
Doegena Fennich
8,974 Points

Ey Jason ,

glad to hear that you solved to problem! The code Guil provided was the right way to do it, i myself forgot to add the if statement for guessLess. edited the previous answer, it should be good now. : D

Cheers