How Blue Can You Get? explainer

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.

Hi!

must it not be
drawBox(rgbObject.blue);

Thanks for your answer.

Albert

Hey there,

drawBox() takes an object that has a .red, .green, and .blue property. All of these properties contain a number between 0 and 255, which determines how much red, green, or blue a color will have.

For example, a color that is purely red and very bright will be:

{
  red: 255,
  green: 0,
  blue: 0
}

While the color black will be:

{
  red: 0,
  green: 0,
  blue: 0
}

and white would be:

{
  red: 255,
  green: 255,
  blue: 255
}

drawBox() needs all 3 of these properties to know what color to create, which is why we pass in the entire rgbObject.

Hope this helps!
Ben

Thanks Ben! :grinning: