Can't understand array set expression

Hello,

In the fcc example, why was the number 3 chosen within a set of arrays (since it represents the fourth row)?

const arr = [
  [1, 2, 3],
  [4, 5, 6],
  [7, 8, 9],
  **[[10, 11, 12], 13, 14]** 
];

arr[3];
arr[3][0];
arr[3][0][1];

My second question is: Why does console.log() not give me an answer in the set of arrays, of what the answer of the element is, in the example?

Would you please provide a link to the challenge?

Arrays in JavaScript (and many other languages) are 0 indexed. So the index of the 4th element of the array arr is 3 which is also a multidimensional array.

For me, flattening it out a bit helps me see it better…

const arr = [  [1, 2, 3],   [4, 5, 6],   [7, 8, 9], [   [10, 11, 12], 13, 14]  ];
      //index:     0             1              2         3

Not sure what you mean by

works fine for me, check your syntax…

console.log(arr[3]);
console.log(arr[3][0]);
console.log(arr[3][0][1]);

output is:

[[10, 11, 12], 13, 14]
[10, 11, 12]
11
1 Like

I don’t think there is any particular reason. The example is showing you how to access multi-dimensional arrays and used the fourth item in arr probably because it is the most interesting since it has two levels of sub arrays.

1 Like

It’s kind of like one person lives at 123 Mainstreet…

another person lives at 456 Grand Apartment 5…

and yet another person lives at 678 2nd Ave Building 8, Unit 9…

indexing is kind of a way of addressing where the element is in the array, and so you have to go deeper into a multidimensional array to access each element within it.

Not a perfect analogy, but maybe it helps?

1 Like

Thanks,

Though i trying to solve for 8…so i’m guessing based on your analogy it’s; arr[2][0][1] ?

It would be 0–> 1 → 2 to get to [7,8,9] (which is itself an array and an element of arr)… so arr[2] for that to get to the 2nd element in the array…

but since that element is also an array, then within that array at

arr[2]

7 is the 0th element of that array, 8 is the 1st

0–> 1 to get to 8

so

arr[2][1]

You start over at index 0 of the nested array because you access its elements the same way

1 Like

Got it, thanks! That was well broken down.

1 Like

…yeah i thought of it as fourth, but logically i should of started counting from the top. Thx

These are confusing for me at first glance too sometimes, so I just take my time and methodically count through it :smiley:

Sounds like you got it now! :+1:

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.