Time to Go explainer

The aim of this puzzle: Add all of the individual elements of flights, trains, and buses into the departures array.

Walkthrough of the solution: 3 arrays are imported from the 'grasshopper.travel' module. A new array called departures is created and the 3 arrays (flights, trains, and buses) are added to it. Then the For Loop logs each item in the new departures array. Notice that the first set of times are grouped together as well as the last set of times. This is because they are still grouped as their own arrays inside of the big departures array.

The departures looks something like this:

[
  ['8am', '12pm', '6am'],
  '7pm',
  '11am',
  '3pm',
  ['10am', '2pm', '4pm']
]

You’ll see that the departures array only has 5 items: the 1st array, 3 train times, then the last array.

In order to expand the inner arrays into their individual elements, we can use the spread operator (...). In the creation of the departures array, the trains array has 3 dots in front of it. That will basically remove the outer brackets [] of the array before adding it to departures.

Add the spread operator (...) to flights and buses so their elements get spread out into departures just like ...trains.

Sample code solution:
(Tap below to reveal)

import { flights, trains, buses } from 'grasshopper.travel';

let departures = [
  ...flights,
  ...trains,
  ...buses
];

for (let time of departures) {
  console.log(time);
}
console.log(departures.length + ' total times');

JavaScript Concepts: Code Block (for loop), console.log(), Data Structures (arrays), import, Spread Operator (...), Variable Scope (let)

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

let flights = ['8am', '12pm', '6am'];
let trains = ['7pm', '11am', '3pm'];
let buses = ['10am', '2pm', '4pm'];  
1 Like

it is correct but the app dont execute the code i m stuck

it s ok …the app was stuck for the language.I changed to english and it worked.