Skip to content

Instantly share code, notes, and snippets.

@ross-u
Last active July 5, 2024 13:09
Show Gist options
  • Save ross-u/9704edbf94fad12b428e539a34dfa47b to your computer and use it in GitHub Desktop.
Save ross-u/9704edbf94fad12b428e539a34dfa47b to your computer and use it in GitHub Desktop.
JS | Array methods - map() & forEach() exercise

JS | Array methods - map() & forEach()


Capitalize the Capitals - Exercise


img


For the following exercise you can use ES5 or ES6 syntax, whichever you feel more comfortable with at this point.

Go ahead to Repl.it , using the code below as your starting point.

Task 1

Given an array of cities, using map() method, return/create an array which will include all the cities having only the first letter of each city name capitalized.


Task 2

Using the forEach() loop, console.log the names of all the elements in the citiesCapitalized array including the number representing the position of the element starting with 1 for the first (index) element.

// Array of cities
const cities = [
"miami",
"barcelona",
"madrid",
"amsterdam",
"berlin",
"sao paulo",
"lisbon",
"mexico city",
"paris"
];
// Task 1
const citiesCapitalized = cities.map(/* Your code Here*/);
console.log(citiesCapitalized);
/* Expected Ouput:
[
"Miami",
"Barcelona",
"Madrid",
"Amsterdam",
"Berlin",
"Sao paulo",
"Lisbon",
"Mexico city",
"Paris"
];
*/
// Task 2
// Your code here
/* Expected Ouput:
[
"1. Miami",
"2. Barcelona",
"3. Madrid",
"4. Amsterdam",
"5. Berlin",
"6. Sao paulo",
"7. Lisbon",
"8. Mexico city",
"9. Paris"
];
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment