A New Place explainer


The aim of this puzzle: Update the boatAtlanta object and then add it to the travelToCities array.

Walkthrough of the solution: The 1st line will import an array called travelToCities from the grasshopper.travel module. The items in this array are objects, and each one has 4 properties: destination, transportMode, cost, and country.

The last variable will store the final object in the array. We can see that the destination of the last object is 'Springfield' by logging it to the console.

The boatAtlanta object is created, and it has the 4 properties needed, except we need to change 2 of its values. The destination should be 'Atlanta', and the transportMode should be 'boat'. We simply edit the 'Seattle' string to 'Atlanta' and the 'car' string to 'boat'.

The boatAtlanta object will be “pushed” onto the travelToCities array, which means it is added to the end.

Finally, the code updates the last variable so that it is storing the new “last object” in the array, and logs out the new final destination.

Sample code solution:
(Tap below to reveal)

import { travelToCities } from 'grasshopper.travel';

let last = travelToCities[travelToCities.length - 1];

let boatAtlanta = {
   destination: 'Atlanta',
   transportMode: 'boat',
   cost: 200,
   country: 'Hoptopia'

last = travelToCities[travelToCities.length - 1];

JavaScript Concepts: console.log(), Data Structures (arrays, objects), import, Indexing, .length(), .push(), Variable Scope (let)


Could you explain to me what the meaning of travelToCities(travelToCities.length-1) ?



travelToCities is an array.
travelToCities.length is the number of items in the array.

Since the numbering starts from 0, the last item in the array is 1 less than the length
travelToCities.length - 1 is the index of the last item in travelToCities

travelToCities.length[travelToCities - 1] is the actual last item in the array, not just its index.



Oh, is that mean .length method count real item on the List ? I thought When we use .length methode the result is same with last index number.


The problem with this exercise is that it introduces 2 new things at the same time. In the first chapter we didnt have to use .length - 1 to print something. Even though it’s not the point of the exercise it’s added in there with no explanation. Are we going to continue to use the .length property with arrays from now on or are they only used in special situations like when an array is imported or has had multiple things added on with .push? I’m just brushing up on coding after a 2 year hiatus and this is really bugging me.


Hey there, thanks for the feedback. I agree that it could be explained further in the puzzle.

For now, I can explain it here. Let’s take a look at the following array:

var animals = ['puppies', 'kittens', 'mogwai'];

Arrays start at 0, so 'puppies' is at animals[0], 'kittens' is at animals[1], and 'mogwai' is at animals[2].

What if we want to access the last element in an array, but we don’t know how long the array is?

Well, we know that we can use .length to get the length of an array, which means animals.length === 3, as there are 3 elements in animals.

However, there isn’t any element at animals[3]. The last element, 'mogwai', is at animals[2].

The last element in an array will always be at an index that is 1 less than the length.

For example:

animals[animals.length - 1] === animals[3 - 1] === animals[2]

For more practice working with arrays, check out our Array Methods course, which you can access after finishing Fundamentals II.

Hope this helps! Let me know if you have any more questions.