Create complex multi-dimensional arrays - I really don't understand the solution

This has really confused me. Sometimes JavaScript rips my brain out! :confounded:
As far as I understood a, 5-level array would look like this:

let arr = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9, "deep"],
[10, 11, 12, "deeper"],
[13, 14, 15, "deepest"]
];

So why does the solution resemble this structure?

let arr = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9, ["deep"]],
[10, 11, 12, [["deeper"]]],
[13, 14, 15, [[["deepest"]]]
];

The instructions are not to add more levels to each existing level, just to include the strings, and I’m not sure what the purpose of [[ ]] and [[[ ]]] are in the last two levels.

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

this is a two levels array, you have 5 monodimensional arrays inside the outer array
you can reach all the things inside with only two brackets: arr[3][3] is "deeper"

in the other case if you wanted to get "deeper" you would need more. You get "deeper" with arr[3][3][0][0]

here visual rapresentation of the two arrays: (there was a closing square bracket in the second case that I had to add to get this)


[Obteined using JavaScript Tutor]

1 Like

Thank you so much. I totally misunderstood levels. :man_facepalming:
I’ll explore that JavaScript Tutor.
I solved the challenge in a different way from the solution.

the solution provided is just a way, there is not a definite way to solve this challenge as it says you can add the elements where you want, they just need to be at the indicated level of depth

I’m just glad to get though it.
Thanks again.

JS has new functions for nested arrays, flat and flatMap. You can check them in official documentation.


Wonderful, thank you. :slight_smile: