Sample Video Frame

18: More Arrays

In this exercise you are going to practice accessing different parts of very complicated Arrays. It's important that you can correctly understand how an index into a nested Array will work. The best way to do that is to drill using such an Array in the node shell.

How this works is I have a series of Arrays in the code below. You are to type this code in like normal, and then require it in the node shell so you can interact with it. Once you have that working you will have to figure out what to type to get the answers I get.

The Code

exports.fruit = [
    ['Apples', 12, 'AAA'], ['Oranges', 1, 'B'], 
    ['Pears', 2, 'A'], ['Grapes', 14, 'UR']]; = [
    ['Cadillac', ['Black', 'Big', 34500]],
    ['Corvette', ['Red', 'Little', 1000000]],
    ['Ford', ['Blue', 'Medium', 1234]],
    ['BMW', ['White', 'Baby', 7890]]

exports.languages = [
    ['Python', ['Slow', ['Terrible', 'Mush']]],
    ['JavaSCript', ['Moderate', ['Alright', 'Bizarre']]],
    ['Perl6', ['Moderate', ['Fun', 'Weird']]],
    ['C', ['Fast', ['Annoying', 'Dangerous']]],
    ['Forth', ['Fast', ['Fun', 'Difficult']]],

What You Should See

If you wrote your code correctly, then your node shell session should look something like this:

> let ex18 = require('./ex18')
> ex18.fruit
    [ [ 'Apples', 12, 'AAA' ],
      [ 'Oranges', 1, 'B' ],
        [ 'Pears', 2, 'A' ],
      [ 'Grapes', 14, 'UR' ] ]

Remember that './ex18' would be a file named ex18.js. In my book's code repository I have it as ex18-more-arrays/code.js so if you see me work in the video you'll see a slightly different input but the same results.

The Challenge

I will give you an Array name and a piece of data in the Array. Your job is to figure out what indexing you need to get that information. For example, if I tell you fruit 'AAA' then your answer is fruit[0][2]. You should attempt to do this in your head by looking at the code, then test your guess in the node shell.

fruit challenge

You need to get all of these elements out of the fruit variable:

  • 12
  • 'AAA'
  • 2
  • 'Oranges'
  • 'Grapes'
  • 14
  • 'Apples'

cars challenge

You need to get all of these elements out of the cars variable:

  • 'Big'
  • 'Red'
  • 1234
  • 'White'
  • 7890
  • 'Black'
  • 34500
  • 'Blue'

languages challenge

You need to get all of these elements out of the languages variable:

  • 'Slow'
  • 'Alright'
  • 'Dangerous'
  • 'Fast'
  • 'Difficult'
  • 'Fun'
  • 'Annoying'
  • 'Weird'
  • 'Moderate'

Final Challenge

You now have to figure out what this code spells out:


Don't attempt to run this in node first. Instead, try to work out manually what each line will spell out.

Learn JavaScript Today

Register today for the Early Access course and get the all currently available videos and lessons, plus all future modules for no extra charge.

Still Not Sure? Try the next FREE Lesson!