0

JavaScript Challenge

❓ CHALLENGE #214 let x = 10; let y = 5; x += y -= x *= y; console.log(x); Output: -35 Why is the answer -35 and not 25? I'm a beginner in JavaScript.

1st Jan 2024, 12:42 PM
José Vitor
José Vitor - avatar
3 Answers
+ 4
see the last statement x=x*y, where x's value will be 50. And the statement before it y=y-x, where y's value will be 5-50= -45. And then x =x+y, where x's value willl be 10+(-45) = 10-45 = -35. that's why answer is -35 x=x+y(y-x(x*y)) x=10+y(5-x(10*5)) -> x=10+y(5-50) -> x=10+(-45) -> x=10-45 -> x=-35 I hope your question is resolved
1st Jan 2024, 2:03 PM
Kaushiki Srivastava
Kaushiki Srivastava - avatar
+ 3
Because these expressions are executed from right to left.
2nd Jan 2024, 12:47 AM
Solo
Solo - avatar
0
Thanks Guys!
2nd Jan 2024, 6:05 PM
José Vitor
José Vitor - avatar