A Pinch of Random explainer

The aim of this puzzle: Draw 2 boxes, one above the other, with colors that are randomly generated.

Walkthrough of the solution: pickRandom() randomly selects a value from a list, while color holds a list of different colors. pickRandom(color) will randomly select one color, such as blue or purple.

To make a box have a random color, pickRandom(color) needs to go inside drawBox(), like drawBox(pickRandom(color)). This is called Call Nesting; when a function β€” pickRandom() β€” is put inside another function β€” drawBox(). The function pickRandom(color) runs and a color is selected, this color is then β€œgiven” to the drawBox() function.

If, for example, the random color selected was red, the computer would β€œsee” drawBox(red) rather than drawBoxes(pickRandom(color)). This means a red box would be drawn.

To make the 2 boxes be stacked above and below, instead of side by side, you add a newLine() command in between the 2 drawBox(...) commands.

Sample code solution:
(Tap below to reveal)

drawBox(pickRandom(color));
newLine()
drawBox(pickRandom(color));

JavaScript Concepts: Calling Functions, Call Nesting, Identifiers
Grasshopper Concepts: pickRandom(), drawBox()

1 Like