Travel Choice explainer

Aim of the puzzle: Use the .length array property to count the number of elements in an array.
Walk through of solution: The first line of code in this puzzle imports an array that is declared in additional code. The array looks like this:

listOfDestinations = [
  'America',
  'Canada',
  'Mexico',
  'Nigeria',
  'India',
  'Madagascar'
];

The second line of code declares a new variable, numberOfDestinations, which has the value listOfDestinations.length

The .length property is used to check the number of elements an array. To use it, add .length to the end of an array, like this:

myArray = [0, 1, 2, 3, 4];
console.log(myArray.length);

This will print 5 to the console, since there are 5 elements in myArray.

To complete the puzzle, use console.log() to print numberOfDestinations to the console.

Sample code solution:

import { listOfDestinations } from 'grasshopper.travel';

let numberOfDestinations = listOfDestinations.length;

console.log('There are ' + numberOfDestinations + ' to choose from.');

Javascript Concepts: Array Properties, .length, console.log(), Import, Variable Declarations with Let

Additional Code (hidden code that runs before the puzzle’s code):

let listOfDestinations = ['America', 'Canada', 'Mexico', 'Nigeria', 'India', 'Madagascar']
1 Like