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

Richard Verbraak
Richard Verbraak
7,739 Points

Confused about the correctGuess = true;

Hi there,

I've had trouble picking this up and I think I'm close to understanding it fully. Yet one thing that doesn't seem crystal clear to me is the correctGuess = true; part.

Why is the correctGuess = true; added to the code beneath? I thought the IF statement always started out as a Yes/True and the Else clause as a No/False. It looks unnecessary to me atleast, to add that part.

if (parseInt(guess) === randomNumber ) { correctGuess = true; }

Isn't the If (parseInt(guess) === randomNumber) already stating that it's true? The same goes for the other lines of code with if statements and adding the correctGuess = true part.

I hope I've explained my confusion enough.

3 Answers

Hello Richard.

Looking at the first statement, the correctGuess variable was initialized with value false. On the next line, the random number was also initialized and a random valua was also assigned to it.

Now, I believe the tricky part comes in on the fourth line with the first if statement. Understand that this if statement will only be executed when the value entered by the user is equal to the randomly generated number. Executing that block of code will change the default/initial value of the correctGuess variable to true.

The second if statement on the seventh line will either execute the if block or the else block depending on the state/value of the correctGuess variable.

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;
} 
if ( correctGuess ) {
    document.write('<p>You guessed the number!</p>');
} else {
    document.write('<p>Sorry. The number was ' + randomNumber + '.</p>');
}

I hope this helps. Feel free to comment again if you still don't understand it.

Richard Verbraak
Richard Verbraak
7,739 Points

Yes, I got it! Thanks for the detailed explanation! The test around the end of the basic JavaScript course made me understand a lot more as well so with this knowledge added I can say that it finally clicked for me. :)

AJ Zhang
AJ Zhang
8,946 Points

Hi! I'm wondering if there is a reason to set the correctGuess value to false in the first line? Would it work the same if it's set to "true"? Thanks.

AJ Zhang
AJ Zhang
8,946 Points

Thanks Omar! I get it now.