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

akash dikkaram
akash dikkaram
1,927 Points

nested else if

In this random number guessing after obtaining input from user if its is wrong(it should go to else but) it does not go through the else statement I can't find where it went wrong.

var guess = prompt("Enter A Value Guessing Between 1 to 10 !"); guessint = parseInt(guess); var random = Math.floor(Math.random()*10)+1; if(guessint===random) { document.write("Guessed Correct"); } else if(guessint>random) { var guessgreat = prompt("Try Again with value LESSER than "+guessint); if(parseInt(guessgreat)===random) { document.write("Guessed Correct great"); } } else if(guessint<random) { var guessless = prompt("Try Again With Valur GREATER than "+guessint) if(parseInt(guessless)===random) { document.write("Guessed Correct Less") } } else { document.write("Oops Guessed wrong"); }

2 Answers

Erik Nuber
Erik Nuber
20,629 Points

Normally that flow

if(){
}else if(){
}else if(){
}else{
}

is correct. However in the case you have created, this flow does not work.

if (guess === random) 

this happens if the guess and random meet

else if (guess > random)

this takes care of the situation when guess is greater than random

else if (guess < random)

this takes care of the situation where the guess is less than random

else

This else will never happen because there are only three possibilities and they have all been covered

Guess can ever only be equal to, greater than or less than. So else will not ever happen.

Of course that is in this an introductory course to condition statements. There are other options like null or NAN but that isn't the point of this particular exercise

in order for your program to work as you have it. you need this

if {

} else if {
    if{

   } else {

   }
} else if {
    if{

   } else {

   }
}
akash dikkaram
akash dikkaram
1,927 Points

thanks @Erik for the solution

Erik Nuber
Erik Nuber
20,629 Points
var guess = prompt("Enter A Value Guessing Between 1 to 10 !"); 
var random = Math.floor(Math.random()*10)+1; 
var guessint = parseInt(guess);

if(guessint===random) { 
        document.write("Guessed Correct"); 
} else if(guessint>random) { 
       var guessgreat = prompt("Try Again with value LESSER than "+guessint); 
         if(parseInt(guessgreat)===random) { 
                 document.write("Guessed Correct great"); 
          } 
} else if(guessint<random) { 
         var guessless = prompt("Try Again With Valur GREATER than "+guessint) 
          if(parseInt(guessless)===random) { 
               document.write("Guessed Correct Less"); 
          } 
} else { 
document.write("Oops Guessed wrong");
 }

The reason is the logic of your if/else statement.

If you guess correctly you get "guessed correct"

else if number 1 checks to see if the number is greater. If so, it asks again and you make a guess. However, once it checks that if the answer is correct it rights Guessed Correct but there is nothing happening if the guess is then wrong.

The same for the next else if. If the number guessed isn't greater than random, it checks to now see if it is lower. If it is, is prompts and again checks to see the guess. If you got it great you print out Guessed Correct Less. However again, if the number isn't right nothing else happens

finally, the else statement will never run because you have covered all conditions. That is if the guessed number is equal to less than or greater than.

To fix, you will have to create else statements within each other check. As in one for if guessGreat === random and one for guessless === random.

akash dikkaram
akash dikkaram
1,927 Points

thanks i get your point

if(){
}else if(){
}else if(){
}else{
}

this is the syntax so when something goes wrong in if it passes to next else if and on next else if and else ain't that how it works please explain where i am wrong thanks