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

Not subtracting 1 from number of questions. Says '3 questions left' every time. What am I missing?

var questions=3;
var questionsLeft=' ['+ questions + ' questions left]';
var adjective = prompt('Please type an adjective' + questionsLeft );
questions -= 1;

Edited For Readability By Dane E. Parchment Jr. (Moderator)

2 Answers

Well, at the end of the code you have written I expect the value of questions will be 2, but since this code is not in a loop it will not iterate over itself and continue to decrement questions. Perhaps a while loop is appropriate here? Something like:

var questions = 3;
while (questions > 0) {
    // do your stuff here
    questions -= 1;
};
Dane Parchment
Dane Parchment
Treehouse Moderator 11,077 Points

While that is the way to solve the problem, that isn't what his issue actually is. Look carefully at his code, and you will notice that he prints out the value of questions before he actually subtracts the value.

Dane Parchment
MOD
Dane Parchment
Treehouse Moderator 11,077 Points

Brittney the problem that you are running into is actually the way that you ordered your code. Look carefully, notice how you call for a variable to be equal to the value of questions, before you actually subtract it? Remember for the most part code executes in order (functions/modules/classes are an exception), and your current order has questions being subtracted after you have called it, meaning that in those variables that access the question variable, they are receiving the value before it is modified.

Now as per Peter's response, a loop would be the best way to go about implementing the actual script you are writing. In the same format as Peter's, but make sure you order your code correctly as per my instructions.

Hopefully this little hint can help you solve it, if you still really need help, let me know and I will provide the fixed code for you. (though I advise you to try and fix it yourself)