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 trialakash dikkaram
1,927 PointsConditional 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
4,882 PointsHi 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!