+ 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.
9 Respostas
+ 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
+ 3
Well it's basically (9+1+1)*9 which is 99.
+ 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
+ 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
+ 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
+ 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
+ 1
You should see in my previous comment that y=9 and x=10 on the second line. Then ++x makes it 11.
+ 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.
+ 1
Exactly