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

what is the main purpose of Var correctGuess = false?

the program works fine without creating the below variable so what did we create it in the first place ?? var= correctGuess = false;

was the main purpose of it to break the if statements ?

or is there any other purpose for it ?

i just need to see it clearly in my head so i gave this "ask a q" a try ! appreciate your help

kevinkrato
kevinkrato
5,452 Points

it sets the condition to be false so later, when you write a conditional statement, it has the opportunity to change the Boolean from 'false' to 'true'. Sort of like and on-off switch. Additionally, if used in a conditional statement, if the value is false, it can cause a series of follow-up actions that would otherwise be ignored if it was true.

It's confusing, but continue the lessons and review it and it will make more sense as it's used in practice.

1 Answer

Ari Misha
Ari Misha
19,323 Points

Hiya there! You are initializing the variable correctGuess in your code by assigning it a default value. Why do we do that? I know its weird but stay with me here. In latter stages of your code, when you'll need this variable , lets just say in loop, its going to throw you a referenceError coz JavaScript interpreter went out to find the variable in your code but couldnt find one so it threw this error to let ya know that to reference this variable you're gonna have to initialize in your code first.

~ Ari