Sales funnel content here linking back to main marketing website Code Institute
The Library
    • All Posts

    • Bootstrap
    • Careers
    • Django
    • Git & GitHub
    • Heroku
    • IDE
    • JavaScript
    • MongoDB
    • README.md
    • Resources
    • Troubleshooting
    • Account

    • Log in
    • Create account

Multidimentional arrays Challenge hint

Written by: anna_ci
|
Published: Aug. 17, 2021
JavaScript

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:

Image to accompany post
Click on a thumbnail to enlarge
Modal title
More posts about JavaScript

About the Author

anna_ci

Anna Greaves

Content Developer | Software Developer

Visit Profile

Tags

challenge javascript multidimentionalArray
Code Institute Courses
Info about the difference between the 2 courses here.
  • All Posts |
  • Categories |
  • Search

Policies

  • Terms and Conditions
  • Privacy Policy
  • Plagiarism Policy
  • FAQs

About

Lorem ipsum dolor sit amet consectetur adipisicing elit. Soluta, omnis fuga cupiditate sit perferendis culpa at vitae officiis molestias, dolores nihil repellendus odio? Praesentium voluptas iusto numquam incidunt error unde?