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 trialWally Mostafa
Courses Plus Student 982 PointsDeclaring 'var correctGuess = false' at the beginning of the code, why do we need to lead with it?
I'm just curious why we need to lead with 'var correctGuess = false;" in this code when it seems that if we are declaring correctGuess = true after in many instances it should default to the false outcome? We clearly need to lead with var correctGuess = false, or else the program fails to run, but I'm just not wrapping my head around why we need it upfront.
Thank you very much for the help!
2 Answers
Karim Bostan
20,430 PointsHi,
I'm still in the learning stage of JavaScript.. and also it took some time for me to understand this at first .. I'll try to calirfy things as I understand it and hope that helps.. and sorry for my bad English..
So,
First, It's important that know the purpose of the program you build.. that will help you understand the code you use..
Second, When you run JavaScript program, it changes continually. It goes in certain steps from up to down and every step could consist of some information and conditions and you want to keep track of these so you can control the workflow of the program and what it produces...
You store these information and conditions in variables.. why? so you can access and change their values at any time you need to..
var variableName = value;
In other meaning, the program status always changes depending on the information and conditions in it so you need to track how they work from start to end...
So for this example here:
Purpose - you want to check the status of the answer from user input whether it's true or false. ( Boolean Value ) and we need to keep track of that answer.
Remember that every thing goes from up to down.
When this program runs at first, the user hasn't entered any thing yet so we trait the answer value as false..
It's like when you start a game .. you start with score = 0 .. then it changes over time as you advance and accomplish certain tasks in the game. so you store the score value at start of the game in variable like this: var score = 0;
/*
- We store the program status at the start., we create variable called "correctGuess" to store
the result of user answer.
- We give this variable value " false ".
- If the user input is right as the generated number, we can change it's value later on in
the program to " true ".
*/
var correctGuess = false;
/*
- Then We generate random number from 1 to 6. We store the generated number in variable
called " randomNumber ".
*/
var randomNumber = Math.floor(Math.random() * 6 ) + 1;
/*
- We need to capture user input now. We store the answer in variable called " guess ".
*/
var guess = prompt('I am thinking of a number between 1 and 6. What is it?');
/*
- Here, We need to check if the user answer is equal to the generated number. So, in case
they are.. the " correctGuess " variable value changes to " true ".
*/
if (parseInt(guess) === randomNumber ) {
correctGuess = true;
}
/*
- So, if the " correctGuess " is " true ", a message is typed to the page
saying " You guessed the number! "
- If not, another message is typed to the page
saying "Sorry. The number was ' + randomNumber + '."
*/
if ( correctGuess ) {
document.write('<p>You guessed the number!</p>');
} else {
document.write('<p>Sorry. The number was ' + randomNumber + '.</p>');
}
Anthony McCormick
6,774 PointsThe first var correctGuess is creating the variable correctGuess and setting its initial state to be false. This state is then changed once the condition of the if statement is met.
Wally Mostafa
Courses Plus Student 982 PointsThanks Anthony, appreciate you taking the time to talk me through that. Makes a bit more sense for sure now.
Wally Mostafa
Courses Plus Student 982 PointsWally Mostafa
Courses Plus Student 982 PointsThis is awesome! Thanks for taking the time to detail this out for me, Karim. Really appreciate the help. Clarified a ton.