Binary Branching explainer


#1

The aim of this puzzle: Use if statements to control which block of code runs.

Walkthrough of the solution: An if statement tests is used to run a block of code only if a certain condition is true. It looks like this:

if (this is true) {
  do this;
}

In this puzzle, the value variable uses the pickRandom() function to choose between 1 and 0. The 1st if statement tests if value === 0. If this is true, print('zero') runs. If value is not 0, then print('zero') will not run.

To complete the puzzle, add a 2nd if statement like the first, but test if value === 1. Inside the code block {}, add print('one').

Sample code solution:
(Tap below to reveal)

var value = pickRandom([0, 1]);
print(value);

if (value === 0) {
    print('zero');
}

if (value === 1) {
    print('one');
}


JavaScript Concepts: Binary Expression (===), Calling Functions, Conditionals (if statement), Data Structures (Arrays), Identifiers, Variable Declaration
Grasshopper Concepts: pickRandom(), print()


listed #2