Plus and Minus explainer

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

1 Like

This makes no sense to me

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

1 Like

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?

1 Like

Can you post a screenshot of your code?

–Frankie

1 Like

Me either…I’m so confused by this

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'

2 Likes

I’m so glad I’m not the only one who’s confused.

Well, I thought that the answer was 10 but when I printed 10 it wasn’t right. So I’m still confused.

Hey there,

Let’s look at the following example:

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

On the 1st line, z is 5.

On the 2nd line, z is 10.

On the 3rd line, the message 'z is 10' is printed.

On the 4th line, z is 7.

On the 5th line, the message 'z is now 7' is printed.

Hope that helps. Let me know if you have any questions.
Ben

1 Like

I finally got it figured out and moved on. Thank you for your help.

2 Likes

This problem is hard

2 Likes

Hey there,

Take a look at this post (it’s the one highlighted in yellow). This explains what the code is doing line by line.

Let me know if you have any questions, and I’ll be happy to answer them!
Ben

1 Like

1 Like

For this first task on operators I literally copy and pasted the solution given and the code still did not work? I’m really confused as what to do?

1 Like

Hey there, I moved your post into the thread for the puzzle. Try posting a screenshot of your code, and I can take a look.

1 Like