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

Alex Tiburzi
Alex Tiburzi
1,179 Points

Why does it say correctGuess = false; ? Shouldn't the correct guess always be true?

Kind of confused about this but I'm sure there's an easy explanation

3 Answers

andren
andren
28,558 Points

The correctGuess variable is used to check if a correct guess has happened. When a correct guess occurs it is set to true. If you set it to true from the start then the code would act as if the guess was correct even if it was not.

Jacques Retief
Jacques Retief
954 Points

This should be your reference point to your question:

if ( correctGuess ) {
    document.write('<p>You guessed the number!</p>');
} else {
    document.write('<p>Sorry. The number is ' + randomNumber + '.</p>');
}

Go from here and place the under understanding of it into context of your question, so that you may come up with your own answer :)

If correctGuess were set to true from the start it would run through the first if statement and go to the end of the program, because the statement would always be true.