Multi-dimensional arrays

Tell us what’s happening:
I need explanation on the code below. Why does console.log return deepest-est? i can’t seem to figure that out.

let nestedArray = [
  ['deep'],[['deeper'], ['deeper']],[[['deepest'], ['deepest']],[[['deepest-est?']]]
];
console.log(nestedArray[2][1][0][0][0]);
// logs: deepest-est

Your code so far


function forecast(arr) {
  // change code below this line
 arr.slice(2,4);
  return arr;
}

// do not change code below this line
console.log(forecast(['cold', 'rainy', 'warm', 'sunny', 'cool', 'thunderstorms']));

Your browser information:

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

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-data-structures/copy-array-items-using-slice/

I believe there’s a missing ] but here’s a general explanation of the steps to take:

So we have this array

let nestedArray = [
[‘deep’],[[‘deeper’], [‘deeper’]],[[[‘deepest’], [‘deepest’]],[[[‘deepest-est?’]]]
];

and we’re accessing this pocket in the array: [2][1][0][0][0]

so first we go to position [2] which means this: [[[‘deepest’], [‘deepest’]],[[[‘deepest-est?’]]]

keep on going like that until you reach the last position [0]

you’re missing a closing bracket btw. But anyway, if I add that in

nestedArray[2][1][0][0][0]
// nestedArray[2], so third item in the original array:
[[['deepest'],['deepest']],[[['deepest-est?']]]][1][0][0][0]
// second item in that array
[[['deepest-est?']]][0][0][0]
// first item in that array
[['deepest-est?']][0][0]
// first item in that array
['deepest-est?'][0]
// first item in that array
'deepest-est?'
2 Likes

Thank you much. I now understand.