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()


Checkerboard help
#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


#5

Why does it not know that zero is black?Screenshot_20180427-112340


#6

Hello,
You are on the right track almost there, Try closing out your colors array.

Example:

var colors = [
‘black’,
‘white’,
];

Then you can use the drawBox functions with color index references. Have fun coding :slight_smile:


#7

I tried that it would not work what’s the problem?


#8

What did you try? What kind of error or message are you getting?

The tricky part of this puzzle is making sure all the brackets and parentheses are in the correct place. Luckily, the starter code has all of these in the right order when you start, so all you have to change are the numbers inside of the brackets []. You can reset the puzzle by opening button keyboard and then tapping the circular arrows button.

–Frankie


#9

I got that one too:grinning:


#10

Why can’t I enable the brackets???

I keep getting it wrong. It’s okay to make fun of me since I’m so stupid anyway!


#11

There are 2 different bracket keys: [ ] and ◂ []

[ ] : Create a new array
◂ [] : Get a specific element from an array. This button gets attached to the name of an array.

In this puzzle you don’t actually need to use either of those keys. You can just change the index number between the brackets.

–Frankie


#12

It’s not working.It says that draw box needs red blue or green


#13

I think you may be seeing the error that says, “drawBox color object must have properties red, green, and blue”.

This happens if the thing inside the parentheses of drawBox() is not a valid color. Perhaps, you have drawBox(colors) in your code.

The colors variable is storing an array of strings, so drawBox(colors) is like drawBox(['black','white']). That won’t work because drawBox() doesn’t know what to do with an array. Try using just 1 item from the array by adding the ◂ [] key to the colors. Then you can put in the index of the item you want (0 for 'black' or 1 for 'white').

–Frankie


#14

I can’t complete the puzzle because the brackets are in the wrong place


#15

You can reset the puzzle using the button on the top row of the keyboard, next to the undo button. From there, you only need to edit the numbers inside the brackets.

–Frankie