The aim of this puzzle: To add to and subtract from the variable z Walkthrough of the solution: This puzzle demonstrates how the plus (+) and minus (-) operators can be used to update a variables value. In the initial code the number 0 is being added to z, and then subtracted from z. To solve this puzzle, change 0 to be a number 1 or greater. Sample code solution:
(Tap below to reveal)
var z = 5;
z = z + 5;
print('z is ' + z);
z = z - 3;
print('z is now ' + z);
I have tried to enter the code as per your instructions in the exercise, but I am now convinced that the code is ironically incorrectly coded, thus making it impossible to solve. How do I complete the exercise when it is incorrectly coded and impossible to solve?
Hey there, hope I can help. Take a look at @Grasshopper_Frankie’s great explanation above. Just in case that doesn’t clarify things, I will also walk through the solution code line by line:
var z = 5
This creates a variable named z and gives it the value 5.
z = z + 5
This line says “Take the value of z, add 5 to it, and then assign that new value to z.” Because z is 5, this is the same as writing z = 5 + 5.
Another way to phrase this is "z is equal to whatever value z has now, plus 5."
z will now have the value 10
print('z is ' + z)
Because z now has the value 10, this will print 'z is 10'
z = z - 3
This line says “Take the value of z, subtract 3 from it, and then assign that new value to z.” Because z is 10, this is the same as writing z = 10 - 3.
print('z is now ' + z)
Because z now has the value 7, this will print 'z is 7'