The aim of this puzzle: Create a red box by getting the color from an array.
Walkthrough of the solution: The beginning of the code creates an array called colors
which just contains 4 strings: 'yellow'
, 'blue'
, 'red'
, and 'green'
.
In JavaScript, the items in an array are numbered from beginning to end, starting at zero and going up just like regular counting. That means 'yellow'
is the 0th item in the colors
array and 'blue'
is the 1st item in the colors
array.
You’re able to choose a specific item from an array using the item’s index which is just a number.
Since you want a red box, you want to get the item at index 2 by using colors[2]
. Now that you know how to access the 'red'
item in the array, you want to use it to draw a redbox – simply put it inside of drawBox()
. That means drawBox(colors[2])
will draw a redbox (as long as you don’t change the colors
array).
Sample code solution:
(Tap below to reveal)
var colors = [
'yellow',
'blue',
'red',
'green'
];
drawBox(colors[2]);
JavaScript Concepts: Calling Functions, Identifiers, Arrays, Indexing, Variable Declaration
Grasshopper Concepts: drawBox()
, newLine()