Multidimentional arrays Challenge hint
Something important to understand about multidimensional arrays, is that a single index in one array, can hold another array as a value.
So for example in this code below:
let myArray = ['A', 'B', [1, 2, 3], 'C']
The value at the 3rd index of myArray is another array with the value of [1, 2, 3]
To get the value of that whole smaller array out of myArray
, you would use the 3rd index to get it. And as indexes start at 0, the 3rd value in myArray
would be accessed using this:
let myNewVar = myArray[2]
Then myNewVar
would hold the value of [1, 2, 3]
As the 3rd value in myArray
is also an array, then in order to get one single number out of the second array, you need a second index. You do this by using another [ ]
value directly after the first.
If you wanted to get the number 2 out of this array:
let myArray = ['A', 'B', [1, 2, 3], 'C']
Then you need two indexes one after the other, the first for the position of the array (at the 3rd index) and then the index of the number 2 within that array, like this:
let myNumber = myArray[2][1]
Using this method, and adding more indexes to your original multidimensional array name, you can drill down into all of the arrays that are nested inside each other for this challenge.
In the image below, I have changed up my array as an example, so I am not giving you the answer to the challenge, but it does make it easier for you to understand the logic. Each of these arrays are nested inside each other. So you need multiple indexes in order to access each array nested inside the previous one until you get to the single value you want in the last nested array.
If I wanted myVar
to be equal to 9, this is how I would access it: