Checkerboard explainer


#1

The aim of this puzzle: Create a checkerboard pattern using colors from an array.
Walkthrough of the solution: The beginning of the code creates an array called colors which just contains two strings: 'black' and 'white'. 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 'black' is the 0th item in the colors array and 'white' 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 the first box of the checkerboard to be black, you need to use the 0th item from the colors array by using colors[0]. Now that you know how to access the 'black' item in the array, you want to use it to draw a black box – simply put it inside of drawBox(). That means drawBox(colors[0]) will draw a black box (as long as you don’t change the first color listed in the colors array!). drawBox(colors[1]) will always draw a box that’s the color of the second item in the colors array. To make your checkerboard pattern, you just need to keep switching between drawBox(colors[0]) and drawBox(colors[1]) and make sure you have a newLine() every three boxes.
Sample code solution:
(Tap below to reveal)

var colors = [
  'black',
  'white'
];
drawBox(colors[0]);
drawBox(colors[1]);
drawBox(colors[0]);
newLine();
drawBox(colors[1]);
drawBox(colors[0]);
drawBox(colors[1]);
newLine();
drawBox(colors[0]);
drawBox(colors[1]);
drawBox(colors[0]);

JavaScript Concepts: Calling Functions, Identifiers, Arrays, Indexing, Variable Declaration
Grasshopper Concepts: drawBox(), newLine()


#2

Antes de um teste deveria ser explicado o tópico. Neste puzzle por exemplo, não havia nada antes sobre “indexing”. Isto faz o aprendizado ser irritante.


#3

Hi I just want to comment that the directions after getting an incorrect answer stating that the colors must be blue or red or green is very misleading


#4

If your code includes drawBox(colors), you’ll get the red error that says: “Error: drawBox color object must have properties red, green, and blue”.

This is confusing because this puzzle comes before learning about objects in the Object Expression section. We’ll change the wording of that error to be less misleading. Thanks for mentioning it!

–Frankie