The aim of this puzzle: Use a For…of loop to draw a rainbow with a different color on each row.
Walkthrough of solution: A For…of loop is set up to loop through an array of colors. On each iteration (loop), the variable x
will refer to the current element in the array. For example, on the 1st iteration, x
has the value 'red'
, so the code in the code block:
drawBox(x);
newLine();
will draw 1 red box before jumping to a new line.
When this puzzle loads, the code block of the For…of loop is empty. To complete the puzzle, call drawBox(x)
, following by newLine()
, so that a rainbow is drawn when the code runs.
Sample code solution:
(Tap below to reveal)
for (var x of [
'red',
'orange',
'yellow',
'green',
'blue',
'indigo',
'violet'
]) {
drawBox(x);
newLine();
}
JavaScript Concepts: Loops, Arrays, Blocks (for loop), Identifiers, Variable Declaration, Calling Functions
Grasshopper Concepts: drawBox(), newLine()