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 Loops, Arrays and Objects Simplify Repetitive Tasks with Loops Exiting Loops

Brian Prouty
Brian Prouty
1,792 Points

Why is the if (correctGuess) at the end is equal to true?

I am wondering why when you call the if (correctGuess) later in the code why is that not equivalent to correctGuess = false; at the top? I was under the impression that the only way to call 'correctGuess = true' is either typing it out like that or '(! correctGuess)'... can anyone help explain this??

https://imgur.com/y3atI6C

Can you post code? I don't see what you are talking about in the video workspace.

Brian Prouty
Brian Prouty
1,792 Points

i just linked it in the question :)

3 Answers

Steven Parker
Steven Parker
231,269 Points

The assignment on line 15 changes the value of "correctGuess" to "true'. That's what happens when the number guessed matches the random number.

The only way for "correctGuess" to still be false at the end is if the loop ends because the count got to 10 and a match was never entered.

Victor Warner
Victor Warner
1,882 Points

if you dont set correct guess to false does that mean later on a conditional statement will assing a boolean value to it?

Steven Parker
Steven Parker
231,269 Points

It is set to "false" to begin with because it might not get set later on.

I am agree with Brian, if the break is present, now the correctGuess variable is useless... am I wrong?

Steven Parker
Steven Parker
231,269 Points

No, it indicates if the loop ended from running out of turns (false) or because the right answer was given (true).