|
1 | 1 | // Remove the unused code that does not contribute to the final console log |
2 | 2 | // The countAndCapitalisePets function should continue to work for any reasonable input it's given, and you shouldn't modify the pets variable. |
3 | 3 |
|
| 4 | +// const pets = ["parrot", "hamster", "horse", "dog", "hamster", "cat", "hamster"]; |
| 5 | +// const capitalisedPets = pets.map((pet) => pet.toUpperCase()); |
| 6 | +// const petsStartingWithH = pets.filter((pet) => pet[0] === "h"); |
| 7 | + |
| 8 | +// function logPets(petsArr) { |
| 9 | +// petsArr.forEach((pet) => console.log(pet)); |
| 10 | +// } |
| 11 | + |
| 12 | +// function countAndCapitalisePets(petsArr) { |
| 13 | +// const petCount = {}; |
| 14 | + |
| 15 | +// petsArr.forEach((pet) => { |
| 16 | +// const capitalisedPet = pet.toUpperCase(); |
| 17 | +// if (petCount[capitalisedPet]) { |
| 18 | +// petCount[capitalisedPet] += 1; |
| 19 | +// } else { |
| 20 | +// petCount[capitalisedPet] = 1; |
| 21 | +// } |
| 22 | +// }); |
| 23 | +// return petCount; |
| 24 | +// } |
| 25 | + |
| 26 | +// const countedPetsStartingWithH = countAndCapitalisePets(petsStartingWithH); |
| 27 | + |
| 28 | +// console.log(countedPetsStartingWithH); // { 'HAMSTER': 3, 'HORSE': 1 } <- Final console log |
| 29 | + |
| 30 | + |
| 31 | + |
| 32 | + |
4 | 33 | const pets = ["parrot", "hamster", "horse", "dog", "hamster", "cat", "hamster"]; |
5 | | -const capitalisedPets = pets.map((pet) => pet.toUpperCase()); |
6 | 34 | const petsStartingWithH = pets.filter((pet) => pet[0] === "h"); |
7 | 35 |
|
8 | | -function logPets(petsArr) { |
9 | | - petsArr.forEach((pet) => console.log(pet)); |
10 | | -} |
11 | | - |
12 | | -function countAndCapitalisePets(petsArr) { |
| 36 | +function countPets(pets) { |
13 | 37 | const petCount = {}; |
14 | 38 |
|
15 | | - petsArr.forEach((pet) => { |
| 39 | + pets.forEach((pet) => { |
16 | 40 | const capitalisedPet = pet.toUpperCase(); |
17 | | - if (petCount[capitalisedPet]) { |
18 | | - petCount[capitalisedPet] += 1; |
19 | | - } else { |
20 | | - petCount[capitalisedPet] = 1; |
21 | | - } |
| 41 | + petCount[capitalisedPet] = (petCount[capitalisedPet] || 0) + 1; |
22 | 42 | }); |
23 | | - return petCount; |
| 43 | + |
| 44 | + return petCount ; |
24 | 45 | } |
25 | 46 |
|
26 | | -const countedPetsStartingWithH = countAndCapitalisePets(petsStartingWithH); |
27 | 47 |
|
28 | | -console.log(countedPetsStartingWithH); // { 'HAMSTER': 3, 'HORSE': 1 } <- Final console log |
| 48 | +console.log(countPets(petsStartingWithH)); // { 'HAMSTER': 3, 'HORSE': 1 } <- Final console log |
0 commit comments