Fix the Data explainer

The aim of this puzzle: Select and output a section of an array using the .slice() method.

Walkthrough of the solution: The transportation array has the string 'train' repeated 3 times in the beginning. We only want 1 'train' listed, which means we should exclude the first 2 items in the array and start our selection on the 3rd item. Since arrays start counting from zero, the 3rd item has an index of 2.

The 1st argument of the .slice() method is the index of the 1st item you want to keep, so the slice out of the transportation array should start with index 2.

The 2nd argument of the .slice() method is the index that you start excluding again. Since we want all of the items (all the way to index 5), the index we would start excluding is 1 more than 5, which is index 6.

To help you remember the arguments: .slice(a, b) means "start keeping items once you get to index a and then stop including items once you get to b".

Sample code solution:

(Tap below to reveal)


import { transportation } from 'grasshopper.travel';
let cities = [
  'London',
  'London',
  'London',
  'Lagos',
  'Lisbon',
  'Louisville'
];

console.log(cities.slice(2, 6));
console.log(transportation.slice(2, 6));

JavaScript Concepts: console.log(), Data Structures (Arrays), import, .slice(), Variable Scope (let)

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

let transportation = ['train', 'train', 'train', 'aeroplane', 'bus', 'boat']
3 Likes

Hi,
Why does the solution keep a line open? It goes like:
>
London,Lagos,Lisabon,Louiville

Why not:
> London,Lagos,Lisabon,Louiville

Does it have something to do with the import code on the first line?

You can do it in either way

All this while it’s been a piece of cake! I mean, today I did 8 grasshopper lessons starting from Console Loggin’.:star_struck:
But now, for some reason; I am clueless about this puzzle! :no_mouth:

Someone please help? I only need to understand…

Hey there, for a walkthrough of the puzzle, try reading the puzzle explainer at the top of the thread. If you have any questions about it, feel free to ask and I’ll do my best to answer!

1 Like

Umm, I past through it and I’m stuck again!
Please help! The lesson is ‘A New Way’.

What is this grasshopper.travel in the import code