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

Alex Flores
Alex Flores
1,474 Points

Why build the string up again if the "questions -= 1;" statement already re-establishes that variable as one less?

Why would I need to build the questionsLeft string up again if the "questions -= 1;" statement already re-establishes that variable as one less than the first iteration?

Wouldn't the reference to the questionsLeft variable in the verb variable be sufficient or does it have to be built up again because questionsLeft happens earlier in the code than the newly re-established questions variable?

2 Answers

Hi Alex,

The questionsLeft variable doesn't get updated automatically anytime the questions variable gets updated. If you want questionsLeft to have a new value then you have to assign a new value to it.

In general, variables always have the last value that was assigned to them.

I tried a different approach and I discovered that the questions variable does update in each prompt even without creating the questionsLeft variable. I think the questionsLeft variable is just a placeholder or shortcut for ' [' + questions + ' questions left]'. If you didn't create the questionsLeft variable, you have to type in ' questions left]' in every prompt instead of just the questionsLeft variable.

Alex Flores
Alex Flores
1,474 Points

Ah gotcha thanks for clarifying, appreciate it!