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

david mchale
david mchale
1,434 Points

counter -= issues

Cant seem to get my counter to decrement despite declaring it again before the prompting variables. ion my console if i type 'var questions =3' , then type 'questions -=1', the result is 2 so what mistake an i making here??

david mchale
david mchale
1,434 Points

var questions = 3; // define the number of questions var questionsLeft = ' ' + questions + ' questions left'; //questions amount and string

//first prompt var adjective = prompt('Please type an adjective' + questionsLeft);

//second prompt questions -= 1; var verb = prompt('Please type a verb' + questionsLeft);

//third prompt questions -= 1; var noun = prompt('Please type a noun' + questionsLeft);

alert('All done. Ready for the message?');

// total 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);

3 Answers

Christopher Debove
PLUS
Christopher Debove
Courses Plus Student 18,373 Points

The content of the variable questionsLeft is evaluated at the moment you affected it. At this time, the value of questions was 3.

What is stored in questionsLeft is not a reference to the variable questions but the string "3 questions left". If you want the text of questionsLeft to update, you need to update the variable with the new content.

var questions = 3;
var questionsLeft = '['+ questions + ' questions left]'; // [3 questions left]
// Some operations
questions -= 1; // questions = 2
questionsLeft = '['+ questions +' questions left]'; // [2 questions left]

I know it sounds redundant at the moment. But it's because the course use much variable.

The upside of it is that it can show you how to use variable. The downside is that you have to update every variable at each modification.

Christopher Debove
PLUS
Christopher Debove
Courses Plus Student 18,373 Points

You need to update your variable questionsLeft. The string is not updated when you update questions.

So even if the counter of question decrease. The string of questionLeft is always the same value than the first time you set it up.

david mchale
david mchale
1,434 Points

Why doest the 'questionsLeft' variable update AFTER the 'questions' variable is decremented on the line previously? Reason why I couldn't figure out this wasn't working for me, is that I thought I wouldn't have to copy the questionsLeft variable again, that it would automatically update considering the 'questions' variable had been dropped x 1 each time a prompt was run.