How Blue Can You Get? explainer


#1

The aim of this puzzle: Check the value of an object’s property inside of an if statement.
Walkthrough of the solution: In hidden code (that can be seen at the bottom of this post), the variable rgbObject is declared with its red, green and blue properties all set to random values between 0 and 255.

In this puzzle, use an if statement to check if the blue property of rgbObject is greater than 200. In the code block of the if statement, call the drawBox() function with rgbObject as an argument.

When the code runs, the drawBox() function will run only if the rgbObject.blue property is greater than 200.

Sample code solution:
(Tap below to reveal)

print(rgbObject.blue);
if (rgbObject.blue > 200) {
  drawBox(rgbObject);
}

JavaScript Concepts: Code Block (if statement), Calling Functions, Conditionals (>), Data Structures (object), Identifiers, Member Expression, Variable Declaration
Grasshopper Concepts: drawBox(), pickRandom()

Additional Code (hidden code that runs before the puzzle’s code):

var rgbObject = {
  red: pickRandom(256) - 1,
  green: pickRandom(256) - 1,
  blue: pickRandom(256) - 1
};

This sets each property to be a random number between 0 and 255.