Simpler Math explainer


#1

Aim of the puzzle: Use the ++ and -- operators to change the value of a variable.

Walk through of solution: On the first line, the x variable is defined with the value 5. The next line increments (adds 1 to) the x variable. You can see the results printed by the next line.

Now, tap on the x on the next line all by itself. Tap the -- button to decrement (subtract 1 from) the x variable. When you run the code now you will see that the value of x went back down by 1.

Sample code solution:
(Tap below to reveal)

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

Javascript Concepts: Variables (let), Increment (++), Decrement (--)

Grasshopper Concepts: print()


#4

I cannot add to a string inside a bracket . When I type ‘x is now’ I cannot add + x between the end of the string and the bracket.
Hope I have explained my problem so that it can be understood!


#5

OK, no need to reply. I fixed it by not doing this by myself, rather just reverting to the original and adding one operator, which I see now was the intention.


#6

Hi,

I don’t understand from where is coming that string ‘x is” and ‘x is now’.

Probably because of that, that we changed the value of x. But I’m not sure how to use it…

I’m looking forward for your help.
Thank u!
Nina


#7

Btw. I just realized as well, that is a “let”, so what about print in {} if is “let” not a var?

Help, i stuck, totally :frowning:
I mean, that exercise i finished correct, but i don’t understand it really.
Thank u!


#8

The strings 'x is' and 'x is now' are not special. We could have used 'The value of the variable is ' and 'After updating x, it has a value of ', or any other string. Those strings are being combined with value of x using the + symbol. When you use + with a string and a variable, it will try to convert the variable into a string and then combine them to create a single string. In this puzzle, the strings don’t actually change the value of the x variable; they’re just used to help us know what the number being printed out is representing.

–Frankie


#9

With this puzzle, a let and var would behave the same way because there are no separate code blocks {} as you mentioned. If you use let in your code, it doesn’t mean that you need to add curly brackets {}; it only means that if there is a code block, then the let can’t leave it. If a let variable is created and it’s not inside of any curly brackets, then there is no boundary on where that variable can be used, except that you can’t use it before you’ve created it.

In this example code, I’ve written 'yes' where the x can be used, and 'no' where it cannot be used:

'no'
{ //Beginning of the let's code block
  'no, not created yet'
  let x = 0;
  'yes, same block'
  {
    'yes, still inside the bigger block'
  }
  'yes'
} // End of the let's code block
'no'
{
  'no'
}
'no'

–Frankie