Skip to content

Instantly share code, notes, and snippets.

@sandrabosk
Last active January 14, 2021 05:41
Show Gist options
  • Save sandrabosk/9183d5345069765dab6d69f66efb1140 to your computer and use it in GitHub Desktop.
Save sandrabosk/9183d5345069765dab6d69f66efb1140 to your computer and use it in GitHub Desktop.
// ***************************************************************
// 1: Capitalize each element of the array - the whole word:
// ***************************************************************
const fruits = ['pineapple', 'orange', 'mango'];
const capsFruits = fruits.map(oneFruit => {
return oneFruit.toUpperCase();
});
// we can write it in one line:
// const capsFruits = fruits.map(oneFruit => oneFruit.toUpperCase())
//
console.log(`fruits: ${fruits}`); // fruits: pineapple,orange,mango
console.log(`capsFruits: ${capsFruits}`); // capsFruits: PINEAPPLE,ORANGE,MANGO
// ***************************************************************
// 2: Capitalize the first letter of every city. <br>
// _Bonus_: if the city has 2 or more words in the name, capitalize the first letter of each word.
// ***************************************************************
const cities = [
'miami',
'barcelona',
'madrid',
'amsterdam',
'berlin',
'sao paulo',
'lisbon',
'mexico city',
'paris'
];
const capsCities = cities.map(city => {
if (city.includes(' ')) {
let splittedArr = city.split(' ');
const newArr = splittedArr.map(elem => elem.charAt(0).toUpperCase() + elem.slice(1));
return newArr.join(' ');
}
return city[0].toUpperCase() + city.slice(1);
});
console.log(capsCities);
// [ 'Miami',
// 'Barcelona',
// 'Madrid',
// 'Amsterdam',
// 'Berlin',
// 'Sao Paulo',
// 'Lisbon',
// 'Mexico City',
// 'Paris' ]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment