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

Why do we need the last else if statement: (parseInt(guess) > randomNumber)

We know there are three outcomes, the guess is correct, the guess is lower, or the guess is higher. In the code below we tested whether the guess is correct of lower, so can't we just use an else statement run the prompt to set the guessLess var? This seems to be simpler, is there a reason not to do it this way?

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('Sorry. The number I am thinking of was higher, guess again!');
      if (parseInt(guessMore) === randomNumber ) {
        correctGuess = true;
      }
  } else if (parseInt(guess) > randomNumber){
      var guessLess = prompt('Sorry. The number I am thinking of was lower, guess again!');
      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>');
}

I tested the following code, using the else statement instead of else if (parseInt(guess) > randomNumber)and the program works correctly. (Note I added a console.log action so I could see the random number and test both right and wrong second guesses)

var correctGuess = false;
var randomNumber = Math.floor(Math.random() * 6 ) + 1;
console.log(randomNumber);
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('Sorry. The number I am thinking of was higher, guess again!');
      if (parseInt(guessMore) === randomNumber ) {
        correctGuess = true;
      }
  } else {
      var guessLess = prompt('Sorry. The number I am thinking of was lower, guess again!');
      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>');
}
Steven Parker
Steven Parker
243,318 Points

Your posted code will be much easier to read if you 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.

2 Answers

Steven Parker
Steven Parker
243,318 Points

You are quite right. The final "if" is not necessary, and the condition it is testing for is the only possible condition at that point in the code. Your modification makes the code both more compact and efficient.

There, should be good now. The video link from "how to ask good questions" on the Ask a Question page references the wrong key for typing backticks. How do I report that?

Steven Parker
Steven Parker
243,318 Points

The instructions for bug reporting are on the Support page.