-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexercise-2.js
More file actions
34 lines (22 loc) · 1015 Bytes
/
Copy pathexercise-2.js
File metadata and controls
34 lines (22 loc) · 1015 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
26
27
28
29
30
31
32
33
// 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 capitalisedPets = pets.map((pet) => pet.toUpperCase()); - redundant
const petsStartingWithH = pets.filter((pet) => pet[0] === "h");
// function logPets(petsArr) {
// petsArr.forEach((pet) => console.log(pet));
// } - redundant
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