Skip to content

Commit e3208ff

Browse files
committed
docs: document refactoring steps and remove dead code
1 parent ae3b75a commit e3208ff

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

Sprint-3/3-dead-code/exercise-2.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,26 @@
2727

2828
// console.log(countedPetsStartingWithH); // { 'HAMSTER': 3, 'HORSE': 1 } <- Final console log
2929

30+
/**
31+
Removed Unused Global Variables: Deleted const capitalisedPets because the array was being processed
32+
but never used anywhere in the script.
3033
34+
Eliminated Dead Code (Functions): Removed the logPets function since it was never called, keeping
35+
the codebase focused only on the required output.
3136
37+
Simplified Internal Logic:
38+
39+
Renamed the function to countPets for brevity.
40+
41+
Replaced the if/else statement inside the forEach loop with a Logical OR shortcut (|| 0). This
42+
handles both initializing the count and incrementing it in a single line.
43+
44+
Optimized Memory Usage: Removed the intermediate variable countedPetsStartingWithH. Instead,
45+
I passed the function call directly into console.log, as the result didn't need to be stored for further use.
46+
47+
Improved Code Scope: Ensured the function uses its local parameter instead of relying on external
48+
global variables.
49+
*/
3250

3351
const pets = ["parrot", "hamster", "horse", "dog", "hamster", "cat", "hamster"];
3452
const petsStartingWithH = pets.filter((pet) => pet[0] === "h");

0 commit comments

Comments
 (0)