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

theo vg
theo vg
489 Points

Questions -= 1 doesn't work

Hello,

When writing

questions = questions - 1;

my javascript code execute well.

But when writing

questions -= 1;

i have an error.

Bellow is my full code :

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

var questions = questions - 1;
var questionsLeft = ' [' + questions + ' questions left]';
var verb = prompt('Please type a verb' + questionsLeft);

var questions -=1;
var 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);

What's the problem here ? I really don't understand.

1 Answer

Steven Parker
Steven Parker
231,271 Points

You should only declare a variable once, all other references should not have "var" in front of them:

var questions = 3;          // "var" is correct the first time
// ...
questions = questions - 1;  // but after that, don't use "var" again
// ...
questions -= 1;             // same here