Checkerboard explainer

I have an error where whenever I try to put multiple things in one draw box (example: drawbox (colors[1] [0]) ) it will only read the first letter of the color that represents. So if 1is set to white, it will say error : w is not a valid color for draw box

Each set of brackets will try to access a specific item from whatever it is attached to. [0] means, get the first item, and [1] means get the second item.

  • colors[1] will get the second item from colors which is the string 'white'.
  • colors[1][0] will get the first item from colors[1], which was the string 'white', and the first item in the string is the first letter, 'w'.

Another way to look at this is:
( colors[1] )[0]

Since drawBox() can only create 1 box at a time, you need to use 2 drawBox() calls to make 2 boxes.

drawBox(colors[1])
drawBox(colors[0])

–Frankie

Hi All

New to the language of programming and enjoying it.

Please advise if I will come upon a project where just inserting a new line and indicating/defining what my variables are ( 0=black and 1=white), the code will in draw boxes automatically will run?

Not sure if my question makes sense when I viewed the example code(below) I thought we had to insert a new line to define what 0 and 1 was

EXAMPLE CODE

var groceries = ['apples', 'bananas', 'oranges']; var fruit = groceries[1];

The 0 and 1 refer to the location of each item in the array. An item’s location in an array is called its “index”.

Indexing starts at 0, meaning that the first item in an array is at index 0.

In this puzzle, the colors array looks like this:

var colors = ['black', 'white'];

'black' is at colors[0] and 'white' is at colors[1].

Hope this clears things up! Let me know if you have any more questions.
Ben