What is the output of this code? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

What is the output of this code?

What is the output of this code? var x = 9; var y = x++; alert (++x * y); The answer is 99. I got 90. Trying to see how you get 99. I did var x = 9; var y = x++ (9) alert(++x * y) which I translated to (10 * 9) which gave me 90.

2nd Nov 2019, 10:56 PM
Zone
Zone - avatar
9 Answers
+ 5
Zone Please take a look at this code x = 9; // the value of x is 9 y = x++; // the value of y is 9 because x are post increment to 10. Now the values of y is 9 and the value of x is 10. ++x * y ; // at this line the value of x is 11 because pre inrement the x. 11 * 9 = 99 ++x first increment then multiple with y. Regards kiuziu
2nd Nov 2019, 11:49 PM
Kiuziu 💘 Berlin
Kiuziu 💘 Berlin - avatar
+ 3
Well it's basically (9+1+1)*9 which is 99.
2nd Nov 2019, 11:12 PM
Rick Sanchez
Rick Sanchez - avatar
+ 3
What you said is absolutely right. However, x's value was increased to 10 in the previous line. y = x++ //now y=9 and x=10 alert(++x * y) //now x is 11 because 10+1 is 11
2nd Nov 2019, 11:27 PM
Russ
Russ - avatar
+ 3
okay! here is my explanation. yes, the answer is 99 var x=9; var y=x++; #here, it is the post incrementation. so, the value initially will be 9 itself and 9 assigns to y and as the statement terminates value of x becomes 10 as x++ means x=x+1 then, alert(++x*y); #as x value became 10 in the previous statement, ++x refers pre increment which makes x immediately 11 where y=9 by second statement that makes 11*9=99
3rd Nov 2019, 7:33 PM
Ivaturi Puja Ankitha
Ivaturi Puja Ankitha - avatar
+ 1
It's 11 * 9. ++x means a pre-increment so the value of x is incremented before it is used. x++ is post-increment so its value is used before it is incremented. var x = 9; var y = x++ //y=9, x=10 alert(++x * y) // 11 * 9
2nd Nov 2019, 11:03 PM
Russ
Russ - avatar
+ 1
Russ I understand , but I'm still confused as to how we got 11. I thought since var x = 9, ++x would be 9 + 1 = 10
2nd Nov 2019, 11:11 PM
Zone
Zone - avatar
+ 1
You should see in my previous comment that y=9 and x=10 on the second line. Then ++x makes it 11.
2nd Nov 2019, 11:18 PM
Russ
Russ - avatar
+ 1
Russ but I thought when it's a post increment (x++) and the value is used before it's incremented, we just take it as 9. In certain problems I see that to be the case, but then in a problem like this it's not. That's where I'm confused.
2nd Nov 2019, 11:24 PM
Zone
Zone - avatar
+ 1
Exactly
3rd Nov 2019, 7:59 PM
Kiuziu 💘 Berlin
Kiuziu 💘 Berlin - avatar