HELP!Create complex multi-dimensional arrays

Tell us what’s happening:
This is actually the example problem.I’m not able to wrap my head over [2][1][0][0][0]
Can someone tell me what each index represents in the given code?
Because when i tried it out, i was able to get deepest-est with just [2][1]. so why even mention [0][0][0]?

Your code so far

let nestedArray = [ // top, or first level - the outer most array
  ['deep'], // an array within an array, 2 levels of depth
  [
    ['deeper'], ['deeper'] // 2 arrays nested 3 levels deep
  ],
  [
    [
      ['deepest'], ['deepest'] // 2 arrays nested 4 levels deep
    ],
    [
      [
        ['deepest-est?'] // an array nested 5 levels deep
      ]
    ]
  ]
];

console.log(nestedArray[2][1][0][0][0]);
// logs: deepest-est?
Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36.

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-data-structures/create-complex-multi-dimensional-arrays

So each of those brackets is another nested array. The first level, nestedArray, contains an array of three elements, but bear in mind that arrays are zero-based (the first element is indexed to zero). Thus, to get to the deeply nested array that is in the third position, we’d point to nestedArray[2].

Now, within that nestedArray[2], there is another array, of two elements. In order to point to the SECOND of those two, we would now be looking at nestedArray[2][1].

And that array element contains a REALLY deep nested array, but each only has a single element, which would have the index zero in each case: nestedArray[2][1][0] contains an array of one element, and that one (nestedArray[2][1][0][0] contains ANOTHER single-element array.

Finally, that last one, indexed at nestedArray[2][1][0][0][0] contains the text deepest-est?. Each bracket indicates another nested array, and they can nest REALLY deep. As they noted, while it looks convoluted, it isn’t really uncommon. It is a pain from a coder’s eye view to try understand, but it’s pretty clean from a computer’s view.

I set up a repl that runs the following code:

let nestedArray = [ // top, or first level - the outer most array
  ['deep'], // an array within an array, 2 levels of depth
  [
    ['deeper'], ['deeper'] // 2 arrays nested 3 levels deep
  ],
  [
    [
      ['deepest'], ['deepest'] // 2 arrays nested 4 levels deep
    ],
    [
      [
        ['deepest-est?'] // an array nested 5 levels deep
      ]
    ]
  ]
];

console.info("First level, nestedArray[2]: ", nestedArray[2])
console.info("Second level, nestedArray[2][1]:", nestedArray[2][1])
// Note that 'deepest-est?' is nested within three more arrays, so:
console.info("Third level,nestedArray[2][1][0]:", nestedArray[2][1][0])
console.info("Getting closer!");
console.log("Fouth level,nestedArray[2][1][0][0]:", nestedArray[2][1][0][0])
console.log("One more array in the above, and its a single element.")
console.info("FIFTH level,nestedArray[2][1][0][0][0]:", nestedArray[2][1][0][0][0])

3 Likes

You made it super clear. Thanks a lot!

I find that, when I start getting confused about things like nested arrays or nested object properties, it helps to break it down and look at the problem step-by-step.

In this case, for me anyway, it made a lot more sense that way. Glad I could help! :wink:

Yup! Thanks once again.