The aim of this puzzle: Draw 4 randomly colored boxes in a 2 by 2 grid.
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 a 2 by 2 grid of boxes, you need a row of 2 boxes side by side, then a new line, then 2 more boxes side by side.
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()