Learn Introductory JavaScript by Building a Pyramid Generator - Step 24

Tell us what’s happening:

" You should update the last element of the cities array to the string “Mexico City”. Remember that you can access the last element of an array using array[array.length - 1]. "

I don’t understand how/where to use array.length:

Your code so far

let character = 'Hello';
let count = 8;
let rows = ["Naomi", "Quincy", "CamperChan"];

// User Editable Region

let cities = ["London", "New York", "Mumbai"];

let cities = ["London", "New York", "Mexico City"];

console.log(cities);


// User Editable Region

console.log(rows);

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36

Challenge Information:

Learn Introductory JavaScript by Building a Pyramid Generator - Step 24

instead of trying to redeclare the cities variable with another different array,
they would like you to mutate the array.
So after the first line that declared cities, if I wanted to change the first value in the array, I would write:
cities[0] = 'Toronto';

Hopefully you recall this indexing method?
So if you know that length is the length of the array, how can you do something similar to what I did but target the last value in the array instead?

" You should update the last element of the cities array to the string "Mexico City" . "

If my brain’s working right then: “you should update the last element of the ‘cities’ array to the string.”
Then, ‘last element’ meaning Mumbai? And I then have to change it to Mexico City? .–.

yes the -value- of the last element is Mumbai initially.
What’s its index though? (try to use length to describe it)

" Remember that you can access the last element of an array using array[array.length - 1] ."
the length must be 1, but the whole array[array.length - 1] confuses me and I don’t understand it at all, as much as I tried thinking nothing lights up.

what if you try adding this line of code just after your cities line
console.log(cities.length);

what do you think that would print out?
how about
console.log(cities[cities.length-1]);

So, i’m trying to divide everything.

array = cities

console.log(cities[length-1]);

console.log allows me to output messages or values to the console

an array stores multiple values and elements in one variable. These values can be of any data type — meaning I can store a string, number, boolean, and other data types in one variable.

[length - 1] will help me access the last element of the array (cities), which for me is Mumbai.

but console.log(cities[length-1]); should print something. Which I sadly still can’t be able to understand

Can you please give me a slightly different example?

cities is a variable yes.
You’ve defined it already here:

The square bracket notation you used indicates an array type.
The array has 3 values.
The array is zero-indexed. Meaning, the first value is considered the zero-th value, the second is the first, the third is the second (confusing but true).
The values in the array can be accessed with bracket notation like:
cities[0] to get London
cities[1] to get New York etc.

The bracket notations wants a number to be in it. (an index is a number)

What is cities.length? it is a number (earlier I wrote the code wrong, I will fix it soon. I forgot the cities. part)

What is cities.length - 1, that is also a number etc.

Can you show me a visual example?

please read this

mod edit: code removed

Thank you for sticking around and explaining everything to me without revealing the answer. Considering it took me 4 whole hours to understand one test I got it in the end thanks to you!

no problem at all.
I removed the code solution as we want people to read this thread and work out the answer for themselves.
Hope you find arrays easy now!

1 Like