Modifier Math explainer


#1

The aim of this puzzle: To add to and subtract from the variable x
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 x, and then subtracted from x. To solve this puzzle you’ll need to change 0 to be a number higher than that (i.e. 1, or 2, or 3, or 4, etc).
Sample code solution:
(Tap below to reveal)

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

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


#2

Second challenge I’m stuck on.


#3

What part are you stuck on?
A thing to remember is what is x set to?
So initially in the example var x = 5 which sets x to the value of 5.
Then in the next step x = x + 5 which makes it 10 (x = 5 + 5).
The third step is printing that value which is 10.
After that x = x - 0 so that will make x = 10 - 0 (remember x hasn’t been reset) so we need to change the 0 to a 3 so the formula will be x = 10 - 3 which will make it a 7.
Finally it will print what x is now set to.

I hope that explains it to you. Always remember what the variable x = is set to. Good luck and keep up the good work :smile:


#4

Thanks, I was able to get after multiple attempts. I just didn’t understand the process of the ( var x ). It’s top to bottom I think.


#5

I am a beginner learning I am stuck in Modfier match can’t go on my lesson :pray::pray:


#6

Could you just give the answer? I seemed to have done everything right but still stuck.

See below:
var x = 5;
x = x + 2
Print (‘ x is ‘ + 7)
x = x - 2;
Print (‘ x is now’ + 5)

What is wrong here plz?


#7

Hi @YMa,
You are printing the number that x would be rather than printing x itself like:
print('x is now' + x);
This will still print the desired result while making use of variables in case you decide to change the values. such as, x = x + 10 will then print out 15 rather than continuing to print 7.
Hope this helps! :smile:


#8

So I’m completely new to code and it a bit confused. I print everything it told me to but it’s still wrong. Here’s what I printed.

Var X = 5
X = X + 5
Print ( ‘X is’ + X)
X = X - 3
Print ( ‘X is now’ +X)

How ever it prints out.
X is 55
X is 52

If you can tell me what to change to fix it. Thanks


#9

With what you are showing is correct but double check if there are quotes around the variable x. That makes it a sting instead of integer. Good luck :slight_smile:


#10

Yes, it goes from top to bottom for most languages.


#11

Yes what @mysteryguy3039 said.

I will just add this:
The single or double quotes make it a string so
Print(‘x is’ + x);
The text in the single quotes (‘x is’) is just plain text (string) the variable x is not going to be shown there. The + (plus) outside the string is called string concatenate (joins the string to something) so + x will now parse the integer and show it’s current value. Hope that helps. Have fun :slight_smile:


#12

Could someone please explain me what is wrong here? :weary:


#13

Hi @Viktorija_Grigonyte,

It looks like you’re adding a string to x (x = x + '5') instead of adding the number 5 to x (x = x + 5)
When you add a string to an variable, it concatenates and gets added on to the end of the variable, so x = x + '5' ==> x = 55.
If you add a number (integer) however, it will add onto the number itself, so x = x + 5 ==> x = 10.

Hope this helps! :smile:


#14

Hi Ahsen,

I don’t understand why var x=5
then in here: x = x+5 became 10 ?
why the first x is not var x =5?

I got stuck on here… :frowning:

Thank you!


#15

Ah!
So the first statement is setting a variable. Like a placeholder, in our case x.

var x

The = tells it to become something so let x be 5.

We can even write: var x = 5;
As:
var x;
x = 5;

The first is just shorthand for the second. Hope that helps :slight_smile:


#16

Hey @zuppachu,

Maybe it’ll also help if I walk through each part of the code.

var x = 5; This line of code tells the computer there is a variable and it’s name is x. It also tells the computer that x is holding the value 5.

x = x + 5; This line of code first tells the computer that we’re going to update the value x is holding (via the assignment symbol =), then lets the computer know that the new value of x will be the old value x was holding plus 5. So the computer does the math, in this case the old value x was holding as 5, so 5 + 5 = 10. So know, the computer knows that the variable called x is holding the number 10.

Variable’s values can change through the code. When a computer encounters a variable is will look ‘up’ to see the latest value that was assigned to it, and use that value.

H :slight_smile:


#17

Can somebody tell me whats wrong with my code. This keeps showing me an error msg “Try replacing the number 0 in x-0 with 1 or higher”
var x =10;
x=x+5;
print('x is now ’ x);
x=x-3;
print('x is now '+x);


#18

@Sugandha_Mathur sure lets take it line by line:

var x =10; <- This is the initial value of x which is being set to 10 in your case. (we should keep it set to 5).
x=x+5; <- This is now saying x = x + 5 which converts to x = 10 + 5; Hence now x is 15.
print('x is now ’ x); <- The print statement needs a plus to join the string with the variable x. so it should be print('x is now ’ + x); instead.

x=x-3; <- Get the current value of x and minus three from it. So x = 15-3 which is now 12.
print('x is now '+x); <- No errors on this line.

I hope that help. Good luck :slight_smile:


#19

Hey @Sugandha_Mathur,

@Ahsen is right — in the code you shared you’re missing a + in the first print() function to combine the string and the variable x. And you don’t need to update the initial variable declaration to be 10.

However, in Grasshopper it shouldn’t be possible to remove the + symbol between two values, and updating the initial variable to 10 won’t block completion of this puzzle. Would you be able to share a screenshot of your code? As I have a feeling there may be another element missing.

Thanks!
H :slight_smile:


#20

Hi,
print('x is now ’ x)- this was a typo… I get it where i was wrong,i wrote my own code beneath the given code.

Thanks for your help though. :slight_smile: