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) Working With Numbers The Mad Libs Challenge Revisited

H Yang
H Yang
2,066 Points

Why 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
Daniel Box
1,939 Points

Hi 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
H Yang
2,066 Points

If 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?

Hi Henry,

That's correct.

Here's maybe a simpler example to illustrate the concept.

var y = 5;
var x = y + 10; // x has the value 15

y = 10; // the value of y is now changed to 10
// x still has the old value of 15
// it doesn't automatically get updated to reflect the new value of y

// you'd have to reassign x if you wanted it to reflect the current value of y
x = y + 10; // now, x is 20

Does that help or did I misinterpret how you were originally thinking about this?

H Yang
H Yang
2,066 Points

Awesome. Thanks for taking the time to elucidate this for me.

Jacques Retief
Jacques Retief
954 Points

I 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.

fixed 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
Jacques Retief
954 Points

Thank you for helping, Jason. I will work on a response!

Jacques Retief
Jacques Retief
954 Points

I 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);

questionsLeft 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
Jacques Retief
954 Points

I 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.