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 trialDaniel Stepanenko
2,376 PointsAssigning strings to questionLeft over and over again
At 2:10, Dave mentions that says that we need to 'build the questionsLeft string up again' after each time that the value of the questions variable is decremented
Why does a string need to be assigned to the questionsLeft variable after each time the value of the the questions variable is decremented?
Wouldn't the questionsLeft variable being passed to each subsequent prompt function call simply continue refer to the questions variable that is being decremented as the control flow progresses?
I don't see why the questionsLeft variable assignment keep needing to be repeated...
3 Answers
Artie Okun
Courses Plus Student 5,224 PointsIt happens because the the variables updates only when compiler runs on the code line by line. I mean, if you want to update the value of questionsLeft, you need to call the line of definition each time and assign the decreased number of questions. In other words compiler doesn't know to put the new number of questions in the questionsLeft variable until it will run the line again.
Jonathan Grieve
Treehouse Moderator 91,253 PointsYou could probably achieve that with a function or a loop. But there's no such functionality in this program. All we're doing is allowing the programming to run in its normal flow; that is running each statement from top to bottom.
So you have to explicitly give each variable it's new values then display it again.. It's the completely opposite to the DRY philosophy in programming but then, we haven't yet been in introduced to the functions stage of the course. :-)
gyatmnucla
7,618 PointsI don't really get it... I understand you can do the same with a function, but why do you have to define the variable over again ? Isn't it supposed to remember it throughout the document ?
Nir Frank
3,578 Points(I'm very much a beginner, so someone correct me if I'm wrong!)
"Isn't it supposed to remember it throughout the document ?" That's right, it does! But only the first time its used. That is, it's being "set" once and then remembers that value. Take the following as an example:
var x = 3;
var y = "Eat " + x + " apples a day!";
console.log(y);
x = 2;
y = "Eat " + x + " apples a day!";
console.log(y);
We'll see the console print out "Eat 3 apples a day!". If we then update x to hold 2, and invoke a console.log for y again, it will still only remember that x was set to 3. That's as far as it knows, because it was set only once on line 2. In order for us to "refresh" it that x has changed to 2, we'll need to set the variable again, so it can look for x once more and then set itself accordingly.
I hope I was clear enough. I also hope I was right :) But this is how I understand it.