Function generateNumber(input) { input++; input *= 2; return input; input = input + 7;

There is a question in codecademy, answer for this question is 6. May i know how is the answer 6 derived. Thanks in advance, appreciate!

What value will be printed to the console when this code is run?

function generateNumber(input) {  
input++;  
input *= 2;  
return input;  
input = input + 7;
} 

console.log(generateNumber(2));

you could step through the code:
input++ when evaluated gives us: 2 + 1, so now input is 3
input *= 2 when evaluated gives us 3 * 2, so now input 6

this how we got 6

what did you expect?

1 Like

Understood! Thanks!

I did not understand what is

input++;  
input *= 2;  

Like why input++, input plus what exactly.
And input *= 2 i thought it means input is 2 then * 2 so i got 4.

++ is a shorthand to increment the variable by one, could also be written like this:

input += 1

which on itself is a shorthand for:

input = input + 1

given increment by one occurs a lot (loops for example), I understand why JS has a short-hand for it

I see, thanks again!

This explaination works great! Crystal clear.

Don’t understand why input = input + 1

why not? works the same as input += 1. += is shorthand to increase the value of the variable.