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
Bradley Marsh
7,396 PointsHelp understanding the else if statement in this number guessing challenge.
Hi,
I am following the JavaScript basics course at the moment and am getting most of it. But have a question regarding the Improving the Random Number guessing game video.
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('Number is higher than your guess of' + guess + ', try again!');
if (parseInt(guessMore) === randomNumber){
correctGuess = true;
}
} else if (parseInt(guess) > randomNumber) {
var guessMore = prompt('Number is lower than your guess of' + guess + ', try again!');
if (parseInt(guessMore) === randomNumber){
correctGuess = true;
}
}
if ( correctGuess ) {
document.write('<p>You guessed the number!</p>');
} else {
document.write('<p>Sorry. The number was ' + randomNumber + '.</p>');
}
I get that the else if statement allows extra options. However I don't understand in this example why it isn't just looping back to ask the user to guess again and again?
Is it because it adds a 'guessMore' variable? If so, how is that working?
3 Answers
Ashley Carpenter
13,393 PointsHi Bradley, An if else statement is condition, so based on whether the condition is true or false you will go down that path and you won't revisit that code. You will also skip out any code that didn't meet your original condition.
You could run this using a loop. It would go something along the lines of:
var randomNumber = Math.floor(Math.random() * 6 ) + 1;
var guess = prompt('I am thinking of a number between 1 and 6. What is it?');
while(parseInt(guess) != randomNumber ) {
guess = prompt('Your guess was wrong, try again!');
}
If that doesn't clear it up entirely, let me know.
Ashley
Bradley Marsh
7,396 PointsHi Ashley,
Yes that does clear it up thank you!! I completely forgot that only one block is run before moving forward. Not going back unless you tell it too.
I feel dumb now, haha.
Ashley Carpenter
13,393 PointsHi Bradley,
Great, I'm really pleased I could help. If you could mark mine as best answer I'd really appreciate that.
It's all about practice, keep at it and you'll be a whiz in no time at all. And you're close to getting to the real fun stuff too!
Ashley
Bradley Marsh
7,396 PointsHi Ashley,
There is no option to mark yours as Best Answer, only my own, which is odd.
Ashley Carpenter
13,393 PointsSorry Bradley, it's because I put my answer as a comment and not an answer! Now i feel dumb haha. I've copied my comment and made it an answer