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 trialH Yang
2,066 PointsWhy do we keep redefining questionsLeft? Why doesn't it stay defined?
I understand using questions -= -1 to reduce the variable questions as a countdown.
I'd like to know why we have to keep defining the variable questionsLeft =.....
I tried the program without repeating var questionsLeft = "[" + questions + " questions left]";
and it returned NaN.
Thanks!
7 Answers
Daniel Box
1,939 PointsHi Henry,
Although the line looks exactly the same, the value returned to questionsLeft is slightly different due to the variable questions being slightly different ( one less ).
So if we want to display the correct amount of questions left in the prompt we need to assign it the new value each time.
H Yang
2,066 PointsAwesome. Thanks for taking the time to elucidate this for me.
Jacques Retief
954 PointsI have a question also about questionsLeft.
If the var questionsLeft has already been defined and declared as what it is (after =), why repeat all of it per sentence?
Can we not simply do the following, as a calling of the var to function as the var that it is declared as? :
questions -= 1;
questionsLeft;
var promptVar = prompt();
// etc.
I apologize for the lack of line spacing in the above .js coding. I am uneducated.
Jason Anello
Courses Plus Student 94,610 Pointsfixed code formatting
Hi Jacques,
This post will show you how to properly post code in the community: https://teamtreehouse.com/forum/posting-code-to-the-forum
I'm not sure if I understand your question. Could you explain it more?
What was your intention with the line questionsLeft;
?
Jacques Retief
954 PointsThank you for helping, Jason. I will work on a response!
Jacques Retief
954 PointsI was wrong in my question. I assumed questionsLeft, as per @Dave MacFarland's MadLib recreation video, had to be repeated. Repeatedly.
See both code examples. Both work.
//My code
var questions = 3;
var questionsLeft = ' [' + questions + ' questions left]';
var adjective = prompt('Please type an adjective' + questionsLeft);
questions -= 1;
var verb = prompt('Please type a verb' + questionsLeft);
questions -= 1;
var noun = prompt('Please type a noun' + questionsLeft);
alert('All done. Ready for the message?');
var sentence = "<h2>There once was a " + adjective;
sentence += ' programmer who wanted to use JavaScript to ' + verb;
sentence += ' the ' + noun + '.</h2>';
document.write(sentence);
//Dave's code
var questions = 3;
var questionsLeft = ' [' + questions + ' questions left]';
var adjective = prompt('Please type an adjective' + questionsLeft);
questions -= 1;
questionsLeft = ' [' + questions + ' questions left]';
var verb = prompt('Please type a verb' + questionsLeft);
questions -= 1;
questionsLeft = ' [' + questions + ' questions left]';
var noun = prompt('Please type a noun' + questionsLeft);
alert('All done. Ready for the message?');
var sentence = "<h2>There once was a " + adjective;
sentence += ' programmer who wanted to use JavaScript to ' + verb;
sentence += ' the ' + noun + '.</h2>';
document.write(sentence);
Jason Anello
Courses Plus Student 94,610 PointsquestionsLeft does need to be repeated if you want the prompts to be updated.
Both programs will produce the same final output but they will not produce the same prompts.
The purpose of Dave's code is to produce the following prompts:
Please type an adjective [3 questions left]
Please type a verb [2 questions left]
Please type a noun [1 questions left]
The number of questions you are told you have left decreases by 1 after answering each question.
All 3 of your prompts will say "[3 questions left]" because you're not updating the questionsLeft variable.
The idea here is that you decrement the questions variable and then you update the questionsLeft variable to reflect this new value. That way the prompts accurately report how many questions you have left.
Jacques Retief
954 PointsGot it. Thanks
Jacques Retief
954 PointsI am learning JavaScript for the first time, and am very new to programming in general.
To me, what it looks like, is that JavaScript executes line by line according to its "latest" information. Thus, questionsLeft, repeated after each prompt, refers to its latest declaration of its original statement, which has its "inner stuff" (that is, var questions) that has undergone some changes (questions -= 1) and appropriately reflects the changes accordingly.
H Yang
2,066 PointsH Yang
2,066 PointsIf I'm understanding this right, the variable questionsLeft was originally defined as a string with some value of the variable "questions" in it. When we assign a new value to the variable "questions" this does not automatically update the value and definition of questionsLeft?
Jason Anello
Courses Plus Student 94,610 PointsJason Anello
Courses Plus Student 94,610 PointsHi Henry,
That's correct.
Here's maybe a simpler example to illustrate the concept.
Does that help or did I misinterpret how you were originally thinking about this?