Basic Data Structures - Create complex multi-dimensional arrays

Im not understanding this solution and am hoping for some insight/clarification. The solution is listed below.

Task: We have defined a variable, myNestedArray, set equal to an array. Modify myNestedArray, using any combination of strings, numbers, and booleans for data elements, so that it has exactly five levels of depth (remember, the outer-most array is level 1). Somewhere on the third level, include the string deep, on the fourth level, include the string deeper, and on the fifth level, include the string deepest.

My confusion: Isnt myNestedArray already 5 levels deep before any modification? If so, then the only task is to add a string to 3 of those arrays, correct?

Why does the solution require inserting the string in an array for nyNestedArray[2]? As opposed to just adding the string via myNestedArray.push()?

And why does the solution require inserting the string in a nested array for myNestedArray[3]?

Etc for myNestedArray[4]…

As I’m writing this I think I may have had a light bulb moment… Before any modifications, myNestedArray is only 2 levels deep. So when I add [[]] to myNestedArray[4], that officially modifies myNestedArray to 5 levels deep, correct?

If thats correct, then why does the solution still require inserting nested arrays elsewhere? Instead of adding those strings via .push? Or even simpler, just hard code the string into the array?

let myNestedArray = [
  // Only change code below this line
  ['unshift', false, 1, 2, 3, 'complex', 'nested'],
  ['loop', 'shift', 6, 7, 1000, 'method'],
  ['concat', false, true, 'spread', 'array', ['deep']],
  ['mutate', 1327.98, 'splice', 'slice', 'push', [['deeper']]],
  ['iterate', 1.3849, 7, '8.4876', 'arbitrary', 'depth', [[['deepest']]]]
  // Only change code above this line

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:105.0) Gecko/20100101 Firefox/105.0

Challenge: Basic Data Structures - Create complex multi-dimensional arrays

Link to the challenge:

We have blurred this solution so that users who have not completed this challenge can read the discussion in this thread without giving away the solution.

no. it is only two levels deep.

an array of flat arrays.

in order to deepen the array you need to insert more arrays nested within the second level arrays.

But that doesnt explain why the solution requires nesting additional arrays in the other arrays. Why doesnt the solution allow me to add the strings in those other arrays using .push or just hard coding it?

.push() doesn’t add depth to the array unless you push a subarray.

In this case though, the comments are demanding that you only make changes inside of the original array definition, so you cannot use .push().

just noticed this add-on.

yes you could have added the arrays this way. what code did you try that didn’t work? (so long as the string is inside an array which is inside an array which is inside an array to deepen the levels)

so myNestedArray[2].push(“deep”) is valid syntax to add the string “deep” to the 3rd nested array, but just not for this exercise?

That is valid syntax but does not increase how nested the array is. It will insert that string at a second level of nesting, not a third.

I understand that. But by my interpretation, this challenge is only asking to add depth to one of the five nested arrays (in addition to adding the 3 required strings).

However in order to pass, the solution is requiring me to add depth to 2 additional nested arrays. The solution is requiring me to go 5 levels deep at one point, 4 levels deep at a different point and 3 levels deep at another.

myNestedArray should contain exactly one occurrence of the string deep on an array nested 3 levels deep

This syntax:

will not meet this goal.

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