Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript JavaScript Loops, Arrays and Objects Tracking Multiple Items with Arrays Build a Quiz Challenge, Part 1 Solution

Michael Humenick
Michael Humenick
1,138 Points

I do not understand arrays at all

Hey guys, I have been understanding JavaScript just well until I reached this part. Could somebody please explain how arrays work?

Thanks in advance!

Steven Vallarsa
Steven Vallarsa
10,842 Points

Yeah, arrays can be mind-bending, especially when you're dealing with multi-dimensional ones like in this exercise. Arrays are basically a big chunk of memory devoted to a list of items under one variable name.

Think of a one-dimensional array like a train, each car being an index in the array. The engine is the array name and doesn't count in the number, and each train car after is indexed by a number starting at zero. You put things in each car and can access them with the square bracket notation. so instead of a accessing a variable with one variable name, you access it by array name and square brackets arr[0] With more dimensions you keep adding square brackets to access the elements within. arr[0][0], etc... Like accessing room [0] in train car [0] in train arr.

That's probably not helpful, but I'm sure you can find better descriptions online.

1 Answer

Agreed. Arrays can be mind-bending [if you let them]. However, a strong understanding of array basics is important if you ever expect to evolve your Javascript code game beyond a hobby. Try these commands in the Chrome console for yourself. (Remember, '//' are comments ...) You'll want to type everything except the comments.



// Create an array named 'arrayColor' which contains colors!

let arrayColor = ['blue', 'green', 'yellow'];


// Make the console show the array and its contents.


// The above command will show the following output indicating an array with 3 elements.

// > (3) ["blue", "green", "yellow"]


// If you click on the little arrow in front of the #3, you should see something similar to the following.

// (3) ["blue", "green", "yellow"]

// 0: "blue"

// 1: "green"

// 2: "yellow"

// length: 3

// proto: Array(0)


// Show the color of the sky in the console.

console.log('The sky is ' + arrayColor[0]);

// The above command prints 'The sky is blue' to the console.


// Show the color of grass in the console.

console.log('Grass is ' + arrayColor[1]);

// The above command prints 'Grass is green' to the console.


//Show the color of the sun in the console.

console.log('The sun is ' + arrayColor[2]);

// The above command prints 'The sun is yellow' to the console.


Hope that helps.

Want to test your understanding? Write a command to the console using your new array that says 'blue and yellow make green'. Add your answer here and we'll see if you got it.

Good luck.

Best, Frank