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):
let bias = Math.random()<0.75 ? 200 : 0;
let rgbObject = {
red:pickRandom(256) - 1,
green:pickRandom(256) - 1,
blue:bias + pickRandom(256-bias) - 1
};