|
1 | | -// Remove the unused code that does not contribute to the final console log |
2 | | -// The countAndCapitalisePets function should continue to work for any reasonable input it's given, and you shouldn't modify the pets variable. |
3 | | - |
4 | | -//let's start from top to bottom and logically think through each line |
5 | | - |
6 | | -const pets = ["parrot", "hamster", "horse", "dog", "hamster", "cat", "hamster"]; |
7 | | -//above line is needed because it is used by everything else. |
8 | | - |
9 | | -const capitalisedPets = pets.map((pet) => pet.toUpperCase()); |
10 | | -// i am removing above line as "capitalisedPets" is not used anywhere else. |
11 | | - |
12 | | -const petsStartingWithH = pets.filter((pet) => pet[0] === "h"); |
13 | | -//this line is filters the words starting with "h" and is used later so we keep it. |
14 | | - |
15 | | -function logPets(petsArr) { |
16 | | - petsArr.forEach((pet) => console.log(pet)); |
17 | | -} |
18 | | -//i am erasing the above function as "logPets" is not used anywhere else. |
19 | | - |
20 | | -function countAndCapitalisePets(petsArr) { |
21 | | - const petCount = {}; |
22 | | -//keeping this function, as petCount is used below. |
23 | | - |
24 | | - petsArr.forEach((pet) => { |
25 | | - const capitalisedPet = pet.toUpperCase(); |
26 | | - if (petCount[capitalisedPet]) { |
27 | | - petCount[capitalisedPet] += 1; |
28 | | - } else { |
29 | | - petCount[capitalisedPet] = 1; |
30 | | - } |
31 | | - }); |
32 | | - return petCount; |
33 | | -} |
34 | | -//everything above is needed to count how many of each pets there are. |
35 | | - |
36 | | -const countedPetsStartingWithH = countAndCapitalisePets(petsStartingWithH); |
37 | | -//it is used for function call, so needed. |
38 | | - |
39 | | -console.log(countedPetsStartingWithH); // { 'HAMSTER': 3, 'HORSE': 1 } <- Final console log |
40 | | -//we wanna see the result of our machinations so we keep it. |
41 | | - |
42 | | -//THE FOLLOWING CODE IS THE CLEANED UP VERSION. |
43 | | - |
44 | 1 | const pets = ["parrot", "hamster", "horse", "dog", "hamster", "cat", "hamster"]; |
45 | 2 |
|
46 | 3 | const petsStartingWithH = pets.filter((pet) => pet[0] === "h"); |
|
0 commit comments