More or Less explainer

#1

The aim of this puzzle: Print out the original random number value after printing another message.

Walkthrough of the solution: The puzzle begins by creating the value variable and storing a random number. Then there is an if...else statement that checks if the value is greater than 5 or not.

If it is greater than 5, then a new value variable is created. This is the difference between let and var. If we tried to use var value in the beginning, and then var value again inside the If Statement, it would try to overwrite the original var. But using let the value inside the block is separate from the one outside the block. So, to the code inside the block, it sees the let value = 'more than 5' and it doesn’t need to look any further so it ignores the let value = pickRandom(5).

It’s similar for the else block. It creates its own let value so that the original random number value is ignored. But once the if...else is done running, and now the code inside the 2 blocks are not running anymore, the new let value that was storing a string is gone. That means if you use print(value) after the if...else block, then it will print out the original number.

Sample code solution:
(Tap below to reveal)

let value = pickRandom(10);
if (value > 5) {
  let value = 'more than 5';
  print(value);
} else {
  let value = 'less than or equal to 5';
  print(value);
}
print(value);

JavaScript Concepts: let, if...else statements

Grasshopper Concepts: pickRandom(), print()

#2

Except by the previous explanation of “let”, the value only exists in the declaration’s own code block. The way it was explained, any number generated and assigned to “value” should cease to exist past the semicolon. So was that just an incredibly poor explanation in something that is supposedly for beginners?

#3

Hey there,

A code block is the area between these brackets: {}. The semicolon just marks the end of the line. This means that one value variable only exists in the if code block, and the other exists in the else block.

Let me know if you have any more questions.
Ben