JavaScript JavaScript Basics 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

Josh Prakke
PRO
Josh Prakke
Pro Student 4,315 Points

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.