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];
console.log(last.destination);

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

travelToCities.push(boatAtlanta);
last = travelToCities[travelToCities.length - 1];
console.log(last.destination);

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) ?

Thanks

1 Like

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[travelToCities.length - 1] is the actual last item in the array, not just its index.

–Frankie

3 Likes

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.
Thanks…

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.

3 Likes

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.

Ben

8 Likes

I’m having a hard time understanding why it is console.log(last.destination)
Why is the .destination needed?

1 Like

travelToCities is an array containing objects, and last is the last object in that array. Because we want to print out the destination property of that object, we can access it with last.destination.

Hope this helps!
Ben

2 Likes

Does that mean travelToCities contains other objects as well, like the cost and transport mode?
Thanks!

“cost” and “transportMode” are PROPERTIES of an object, not objects.

Hi Grasshopper_Frankie. I feel I understand this topic now; your example of the array of animals made it painless. But Please tell me the following was a typo, else I’ll get a big headache. Near the top you wrote

Should it not be " travelToCities[ travelToCities.length - 1 ] " to be the actual last item in the array?

Thanks

1 Like

Hello, if possible, could I get a reply to the question please? Thanks.

Yes I think you are right, it was some sort of typing mistake. What else could it be ?
travelToCities.length[travelToCities - 1] makes no sense to me.

@ParkleZ Hello and many thanks for the feedback. This is an interesting and friendly place. My point was that, depending on one’s background, when you’re learning more and more unfamiliar complex expressions, that must be typed perfectly for the computer to accept, and the only info comes from the explainer page, as opposed to say classroom and textbooks, then a mere typo can make you wonder! I think it should be corrected (or explained). Onward and upward!

Maybe I needed to address my question, @Grasshopper_Frankie , rather than Hi , Grasshopper_Frankie; perhaps they didn’t see it.

1 Like

Hey there, that is indeed a typo. Good catch! I edited Frankie’s post so it doesn’t confuse anyone else.

You are correct that it should be travelToCities[travelToCities.length - 1]

Ben

Thanks so much. Headache gone.