Rail Crossing explainer


#1

The aim of this puzzle: Use if statements to control what color of box to draw, creating rail crossing light controls.

Walkthrough of the solution: The barrier variable will store a string picked at random. Each time you run the code, it will be either 'down' or 'up'. Then the value will be printed out, so you can see it.

Next, there is an if statement which checks the value of the barrier and compares it to 'up'. If the barrier is storing 'up', then a green box will be drawn. That’s all for that if statement.

Add another if statement at the very bottom of the code, after last }. It should check if barrier === 'down'. And inside of the curly brackets of that new if statement, it should draw a red box.

Sample code solution:
(Tap below to reveal)

var barrier = pickRandom([
  'down',
  'up'
]);
print(barrier);

if (barrier === 'up') {
  drawBox(green);
}

if (barrier === 'down') {
  drawBox(red);
}

JavaScript Concepts: Code Block (if statement), Calling Functions, Conditionals (===), Data Structures (array), Identifiers, Variable Declaration
Grasshopper Concepts: pickRandom(), drawBox()


listed #2