Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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,286 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