Basic Data Structures - Create complex multi-dimensional arrays

I am a bit confused about the question, the example is

let nestedArray = [ //level 0
  ['deep'], //level 1
  [ //level 2
    ['deeper'], ['deeper'] 
    //[2][0], [2][1]
  ],
  [ //level 3
    [
      ['deepest'], ['deepest']
    ],
    [ //level 4
      [
        ['deepest-est?']
      ]
    ]
  ]
];

and description says

The deep array is nested 2 levels deep. The deeper arrays are 3 levels deep. The deepest arrays are 4 levels, and the deepest-est? is 5.

I am a bit confused because from my understanding it is on level 1 and so on. I have commented out how I understand array.

Edit : Here is link to the algo

Hey!

Can you please add a link to the challenge here so its easier for others to help you?

Hi,
I have updated the question to mention the link.

The whole point of this exercise is to help you conceptualize how elements in a multidimensional array are accessed.

in a normal array, you can can access its element by inserting its index in square brackets like this.

let arr = [1, 2, 3]
let thirdArrayElement = index[2] // array index starts at 0

When you have a 2 dimensional arrays, you can do the same thing but you also have to state the number of array you want to access.

let twoDArr = [ ["first", "Array"],["second", "array"] ]
let secondArrayFirstElement = twoDArr[1][0]

It becomes much trickier when you get to 3 dimensional arrays. the main point is to check at each level if you can access the elements that are asked in the tests. for example, the test states “Somewhere on the third level, include the string deep” . Can you access deep on the third level in your code?

Tip: you can access the third level by using three square brackets.

array[0][1][2]

Hope this helps! :smile:

You are right, it is confusing to me as well!
I think it should say: " The array with string deep is nested 1 level deep"

let nestedArray = [
    ['deep'],
    'deep'
]

because you can access it as

nestedArray[0] // => ['deep']

and when you have only string there on the same level, you would access it the same way

nestedArray[1] // => 'deep'

Both elements are on the 1st level.

@staranbeer don’t you think?

Somewhere on the third level, include the string deep

The test explicitly states that you need the string deep on the third level, not the array which contains the string deep.

How can be said that the deep array is nested 2 levels deep when it lies on level 1?
I understand that the author tried to name the array somehow, but he can’t say the array is nested 2 levels deep.

console.log(nestedArray[0]) // => ['deep'] 

The array object is on the level 1.

I think the op paraphrased the question because the original instructions are pretty self explanatory.

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.

I don’t know if this can help


But this is a way of visualizing it
Using JavaScript coding tutor - Learn JavaScript by visualizing code execution

I hope this makes it clearer for you.

The following strings are nested at levels:

let nestedArray = [
  'deep', // nestedArray[x] => level 1
  [ 
    'deeper', 'deeper' // nestedArray[x][y] => level 2
  ],
  [ 
    [
      'deepest', 'deepest' // nestedArray[x][y][z] => level 3
    ],
    [ 
      [
        'deepest-est?', // nestedArray[x][y][z][w] => level 4
       ['deepest-est?'] // this array is also nested on level 4
      ]
    ]
  ]
]

So the string 'deepest-est?' and the array ['deepest-est?'] elements are both nested 4 level deep and you can access them with 4 pairs of square brackets.

// 'deepest-est?'
let deepest_est_string = nestedArray[2][1][0][0]
// ['deepest-est?']
let deepest_est_array = nestedArray[2][1][0][1]

However, if you need to access members of those elements, you need to use the bracket notation again.

// 'd'
deepest_est_string[0] // same as nestedArray[2][1][0][0][0]
// 'deepest-est?'
deepest_est_array[0] // same as nestedArray[2][1][0][1][0]

I still believe @mahassan has a good point here and the numbering of levels in the couple sentences below can be confusing.

The deep array is nested 2 levels deep. The deeper arrays are 3 levels deep. The deepest arrays are 4 levels, and the deepest-est? is 5.

Sorry, should have been little confused, I meant I see array as following. Am i wrong?

yes, you are wrong
I added one comment here

let nestedArray = [
  ['deep'], //[0]
  [ // [1]
    ['deeper'], ['deeper']  //[1][0] and [1][1]
  ],
  [ //[2]
    [ // if the above is [2] what's this
      ['deepest'], ['deepest'] //[2][0] and [2][1]
    ],
    [ //[3]
      [//[3][0]
        ['deepest-est?']//[3][1]
      ]
    ]
  ]
];
let nestedArray = [
    ['deep'], //[0]
    [ // [1]
        ['deeper'],
        ['deeper'] //[1][0] and [1][1]
    ], //[1] ends
    [ //[2]
        [ //[2][0] = array
            ['deepest'],
            ['deepest'] //[2][0][1] and [2][1][1]
        ],
        [ //[2][1] = array
            [ //[2][1][0]
                ['deepest-est?']
            ]
        ]
    ]
];

I think it is making sense to me but what is confusing to me is why we need what I commented as //[2][0] = array and //[2][1] = array when we have [//[2][1][0] which is saying that this is an array but again inside we give it another array of string.

if you want to access the strings, you don’t need to use bracket notation to get each array, you can just use it to get the strings

let nestedArray = [
  [
    'deep' // nestedArray[0][0]
  ],
  [
    [
      'deeper' // nestedArray[1][0][0]
    ],
    [
      'deeper'  // nestedArray[1][1][0]
    ]
  ],
  [
    [
      [
        'deepest' // nestedArray[2][0][0][0]
      ],
      [
        'deepest' // nestedArray[2][0][1][0]
      ] 
    ],
    [
      [
        [
          'deepest-est?' // nestedArray[2][1][0][0][0]
        ]
      ]
    ]
  ]
];

I am confused, so take the example above, why do we need [0][0], why not nestedArray[0] which will give “deep”, also I am confused about question when it say

so that it has exactly five levels of depth (remember, the outer-most array is level 1).

let myNestedArray = [
    // Only change code below this line
    ['unshift', false, 1, 2, 3, 'complex', 'nested'],  //is this level 1? coz I thought it was level 0 as array start as 0
    ['loop', 'shift', 6, 7, 1000, 'method'],
    ['concat', false, true, 'spread', 'array'],
    ['mutate', 1327.98, 'splice', 'slice', 'push'],
    ['iterate', 1.3849, 7, '8.4876', 'arbitrary', 'depth']
    // Only change code above this line
];

sorry If I sound annoying, trying to get the hold of arrays

you can consider the level of nesting to be how many pair of brackets you need to access the elemets.
The outer element is level one because you get its elements with myNestedArray[x], those inside it are level 2 because myNestedArray[x][y] to get the elements inside it.

I like to use this tool to see the structures
https://pythontutor.com/javascript.html

This is an array with two levels of depth.

1 Like

This is my solution but I think description in confusing

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, '8x.4876', 'arbitrary', 'depth','deepest'],
  // Only change code above this line
];

The question asks

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 .

The confusion is it says

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 .

It does not mention the word array of string e.g. ['deep'] as stated in thread help hint
["concat", false, true, "spread", "array", ["deep"]],

array can contain any kind of element, if it contains only strings itàs called “array of strings”

Sorry for replying to this thread after a month, I was doing some courses related to security so was very busy. I can feel my confusion generates from the question which says

The deep array is nested 2 levels deep. The deeper arrays are 3 levels deep. The deepest arrays are 4 levels, and the deepest-est? is 5.
But when I visualize it, it shows like this


This shows that deep array is at level 0, but in the question it says
The deep array is nested 2 levels deep

Sorry but I am totally confused over and question demonstrates the nested array vs. how visualization shows it.