Useful String Methods - Fixing capitalization (exercise)

Hello everybody

I didn’t understand how the slice() method is being used here (code pasted below).

let firstLetter = lower.slice(0,1);

I thought these index would refer to the first and second cities in the array, ‘lonDon’ and ‘ManCHESTer’, respectively. Instead, it is taking the first letter of each word. I am sure I am missing something, but I couldn’t figure it out on my own. I did some research and find nothing about it.

I’d appreciate if someone could enlighten me.
Thank you!


const list = document.querySelector(’.output ul’);
list.innerHTML = ‘’;
let cities = [‘lonDon’, ‘ManCHESTer’, ‘BiRmiNGHAM’, ‘liVERpoOL’];

for (let i = 0; i < cities.length; i++) {
let input = cities[i];
// write your code just below here

let lower = input.toLowerCase();
let firstLetter = lower.slice(0,1);
let capitalize = lower.replace(firstLetter, firstLetter.toUpperCase());
let result = capitalize;

let listItem = document.createElement(‘li’);
listItem.textContent = result;
list.appendChild(listItem);
}

In your case,
lower.slice(0,1); is equivalent to lower[0].
The first input value of the slice() method would be the starting index, and the second input value would be the (stopping index + 1).

Hi Hz7426

Thanks for your reply.

In this case, what should I use to have ‘lonDon’, ‘ManCHESTer’ as the result of a .slice() method?

It seems like the exercise I did was different from yours.
What is the end goal of your exercise? Could you post the exercise link?

Hi

Actually, I was able to finish the exercise. All good!
I was just trying to understand better the .slice().
But I’ll continue the course and come back if other questions arise again.

Thank you!