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 Create a Two Dimensional Array

Kuladej Lertkachonsuk
seal-mask
.a{fill-rule:evenodd;}techdegree
Kuladej Lertkachonsuk
Full Stack JavaScript Techdegree Student 3,175 Points

Please help me on this one.

Please help me fix my code.

script.js
var coordinates = [

[10,20]
[4,0]
[0,0]
[3,10] 

];
index.html
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>JavaScript Objects</title>
</head>
<body>
<script src="script.js"></script>
</body>
</html>

3 Answers

Daan Schouten
Daan Schouten
14,454 Points

Creating a multi dimensional array is exactly the same as creating a regular one, but with a number of arrays instead of a number of values. Recall that a normal array would look like this:

          [1, 2, 3]

Meaning that the simplest array of arrays should look like this:

          [ [1], [2], [3] ]

And that your array should look like this:

         [ [10,20], [4,0], [0,0], [3,10] ]
Jaroslaw Adamowicz
Jaroslaw Adamowicz
11,634 Points

Hi there,

I think the problem is that you are missing comma between your inner arrays.

Simple, empty array will be:

var myArray = []

array with one array (with one item in it):

var myArray = [
[1]
]

Now let's add another array to our myArray with no item:

var myArray = [
[1],
[]
]

NOTE: I used comma to separate inner arrays!

Let's put four numbers to our inner empty array:

var myArray = [
[1],
[1, 2, 3, 4]
]

As yo can see comma is also used for separating elements inside array. It doesn't really matter if this elements are nubers or other arrays.

If I want one array with four arrays in it, and each inner arrays has like 5 numbers, this what would I do:

var myArray = [
[1, 7, 8, 9, 10],
[1, 2, 3, 4, 5],
[0, 0, 0, 0, 0],
[11, 111, 22, 222, 55]
]

Please notice that last item in array is not ended with comma. But in new ECMA script even comma after last element should cause no problem :)

So in the end my code look like this:

var coordinates = [
  [1,2],
  [2,3],
  [3,4],
  [4,5]
]

I hope that helps!

Cheers!

BR,

Jarek