Plus and Minus explainer

#1

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);

JavaScript Concepts: Binary Expression (=, +, -), Calling Functions, Identifiers, Variable Declaration
Grasshopper Concepts: print()

#3

This makes no sense to me

#4

To solve the puzzle, there are 2 things you need to change:

  1. Edit the 1st number 0 into a larger number, like 5
  2. Edit the 2nd number 0 into a larger number, like 3

That will turn your code into the solution code:

var z = 5;
z = z + 5;
print('z is ' + z);
z = z - 3;
print('z is now ' + z);

I’ll explain what each line does.

var z = 5;
“Create a variable (a thing to store data) called z and store the number 5 in it.”

z = z + 5;
“Change the value of z into z + 5. Since z was storing 5, this becomes 10.”

print('z is ' + z);
"Output the text 'z is 10' since the value of z is now 10"

z = z - 3;
“Change the value of z into z - 3. Since z was storing 10, this becomes 7.”

print('z is now ' + z);
"Output the text 'z is 7' since the value of z is now 7"

–Frankie

#5

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?

#6

Can you post a screenshot of your code?

–Frankie

#7

Me either…I’m so confused by this

#8

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'

1 Like