Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

JavaScript

akash dikkaram
akash dikkaram
1,927 Points

Conditional Statement(Improving The Random Number Guessing Game)

I Have Coded This And I Am Not Getting The Output.I Did Not Use Boolean.Can Someone Help Me With This. Is There Anything Wrong With Logic Or Code?? Thank You

var guess=prompt("Enter The Guessed Value Between 1 To 10");
var integer = parseInt(guess);
var random = Math.floor(Math.random()*10)+1;

if(guess===random){

    document.write("<h3> You Have Guessed It Correct The Value Is"+random+"</h3>");
}

else if(guess<random)
    {

      var guessagain=  prompt("Sorry Try Again Random value is greater than what you have guessed");
        if(guessagain===random){

            document.write("You Guesssed It In A Try")
        }


    }
 else if(guess>random)
    {

      var guessless=  prompt("Sorry Try Again Random value is lesser than what you have guessed");
        if(guessless===random){

            document.write("You Guesssed It In A Try again")
        }


    }
else
    {

        document.write("<h3>The Random Value is"+random+"</h3>")
    }


</script>

1 Answer

Marco Amadio
Marco Amadio
4,882 Points

Hi Akash.

When you evaluate guessagain or guessless, you manage only the case in which the user guess the number, but the case in which the number is different from "random" is managed in the outer "else", and will never occur!

You need something like this:

/* ... */

} else if ( guess > random ) {
    var guessless=  prompt("Sorry Try Again Random value is lesser than what you have guessed");
    if ( guessless === random ) {
        document.write("You Guesssed It In A Try again");
    } 
    /* here you need your outer else statement */ 
    else {
        document.write("<h3>The Random Value is" + random + "</h3>");
    }
}

/* ... */

Also, is not part of the error, but: you declare an "integer" variable, but you never use it.

Hope it helps!