Determine the final value of the JavaScript variable x after

Determine the final value of the JavaScript variable x after the following sequence of statements have been executed.

var x = 0, y = 1;
x = x - y;
y = x + y;
x = 3 * x + y;
document.write(\"x = \" + x);

Solution

Answer: -3

Explanation:

var x = 0, y = 1;
x = x - y;
y = x + y;
x = 3 * x + y;
document.write(\"x = \" + x);

At line 1, x value is 0 and y value is 1

at line 2, x = x - y it means x = 0 - 1 so x value is -1 at line 2

at line 3, y = x + y it means y = -1 + 1 so y value is 0 at line 3

at line 4, x = 3 * x + y; it means x = 3 * -1 + 0 so x = -3 + 0 so x value is -3 at line 4.

Determine the final value of the JavaScript variable x after the following sequence of statements have been executed. var x = 0, y = 1; x = x - y; y = x + y; x

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site