-
-
Notifications
You must be signed in to change notification settings - Fork 337
Expand file tree
/
Copy pathexercise-2.js
More file actions
25 lines (20 loc) · 852 Bytes
/
exercise-2.js
File metadata and controls
25 lines (20 loc) · 852 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
// Remove the unused code that does not contribute to the final console log
// The countAndCapitalisePets function should continue to work for any reasonable input it's given, and you shouldn't modify the pets variable.
const pets = ["parrot", "hamster", "horse", "dog", "hamster", "cat", "hamster"];
const petsStartingWithH = pets.filter(
(pet) => pet[0].toLowerCase() === "h"
);
function countAndCapitalisePets(petsArr) {
const petCount = {};
petsArr.forEach((pet) => {
const capitalisedPet = pet.toUpperCase();
if (petCount[capitalisedPet]) {
petCount[capitalisedPet] += 1;
} else {
petCount[capitalisedPet] = 1;
}
});
return petCount;
}
const countedPetsStartingWithH = countAndCapitalisePets(petsStartingWithH);
console.log(countedPetsStartingWithH); // { 'HAMSTER': 3, 'HORSE': 1 } <- Final console log