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

Luqman Shah
Luqman Shah
3,016 Points

confused with this syntax - working with numbers

I'm a little confused... firstly, why is he subtracting from the questions variable like this?:

questions -= 1;

And why not like this?:

questions = - 1;

or like this:

var questionSub = questions - 1; (there is no questionSub variable in the video, I just made it up to give an example)

secondly, for the variable questionsLeft, why are the questions variables in quotation marks if they aren't strings, they are being attached to a string, which is ' questions left'

var questionsLeft = ' [ + questions +' questions left]';

why isn't it written as:

var questionsLeft = questions + ' questions left';

2 Answers

For the first question you had if you were to use:

questions = -1;

It would assign -1 to the variable as apposed to subtracting one from the variable with:

questions -=1;

var questionsSub = questions -1 1

should work as well but why add an extra variable when we can just subtract from the questions variable we already have.

As for the second question he is using the quotation marks in the questionsLeft variable for concatenation. So it would display something along the lines of :

[4 questions Left].

You can use both syntax as: question - = 1 ; question = question -1;

both are valid syntax. However, both the syntax does the same action, and first one (short-hand method) is often used because it require less code to write.

Now, when you put operator after '=' symbol as question =- 1; it does not reduces 1 from variable, it assigns -1 value to that variable again and again.