From c13bb371aa04a10275fdebf0586000237105cc68 Mon Sep 17 00:00:00 2001 From: "Karla G." Date: Thu, 26 Feb 2026 23:30:48 +0000 Subject: [PATCH 1/3] refactor: remove dead code and optimize sayHello function --- Sprint-3/3-dead-code/exercise-1.js | 39 ++++++++++++++++++++++++------ 1 file changed, 31 insertions(+), 8 deletions(-) diff --git a/Sprint-3/3-dead-code/exercise-1.js b/Sprint-3/3-dead-code/exercise-1.js index 4d09f15fa..b539fdb11 100644 --- a/Sprint-3/3-dead-code/exercise-1.js +++ b/Sprint-3/3-dead-code/exercise-1.js @@ -1,17 +1,40 @@ // Find the instances of unreachable and redundant code - remove them! // The sayHello function should continue to work for any reasonable input it's given. -let testName = "Jerry"; + +// DEAD CODE +// let testName = "Jerry"; +// const greeting = "hello"; + +// function sayHello(greeting, name) { +// // const greetingStr = greeting + ", " + name + "!"; +// return `${greeting}, ${name}!`; +// console.log(greetingStr); +// } + +// // testName = "Aman"; + +// const greetingMessage = sayHello(greeting, testName); + +// console.log(greetingMessage); // 'hello, Aman!' + +/** +"Clean Code Refactoring: SayHello Function" + +Removed Dead Data: Eliminated the unreachable variable value "Jerry" to keep memory clean. +Enforced Immutability: Switched from let to const for variables that do not change, preventing accidental reassignments. +Optimized Variable Scope: Reorganized the declaration order to ensure variables are defined before they are called. +Simplified Function Logic: Removed redundant intermediate variables (greetingStr) inside the function to return the result directly. +Streamlined Output: Removed internal console.log statements after testing to keep the console clean and focused on the final output. +Execution: Properly invoked the function by passing the correct parameters for the final print. +*/ + const greeting = "hello"; +const testName = "Aman"; function sayHello(greeting, name) { - const greetingStr = greeting + ", " + name + "!"; return `${greeting}, ${name}!`; - console.log(greetingStr); + } -testName = "Aman"; - -const greetingMessage = sayHello(greeting, testName); - -console.log(greetingMessage); // 'hello, Aman!' +console.log(sayHello(greeting, testName)); // 'hello, Aman!' From ae3b75a7f4c2eb5edb7907f24ee329eb386bf9b7 Mon Sep 17 00:00:00 2001 From: "Karla G." Date: Fri, 27 Feb 2026 00:19:11 +0000 Subject: [PATCH 2/3] feat: remove dead code and redundant operations for better readability --- Sprint-3/3-dead-code/exercise-2.js | 50 +++++++++++++++++++++--------- 1 file changed, 35 insertions(+), 15 deletions(-) diff --git a/Sprint-3/3-dead-code/exercise-2.js b/Sprint-3/3-dead-code/exercise-2.js index 56d7887c4..1eb7dc57f 100644 --- a/Sprint-3/3-dead-code/exercise-2.js +++ b/Sprint-3/3-dead-code/exercise-2.js @@ -1,28 +1,48 @@ // 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()); +// const petsStartingWithH = pets.filter((pet) => pet[0] === "h"); + +// function logPets(petsArr) { +// petsArr.forEach((pet) => console.log(pet)); +// } + +// 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 + + + + const pets = ["parrot", "hamster", "horse", "dog", "hamster", "cat", "hamster"]; -const capitalisedPets = pets.map((pet) => pet.toUpperCase()); const petsStartingWithH = pets.filter((pet) => pet[0] === "h"); -function logPets(petsArr) { - petsArr.forEach((pet) => console.log(pet)); -} - -function countAndCapitalisePets(petsArr) { +function countPets(pets) { const petCount = {}; - petsArr.forEach((pet) => { + pets.forEach((pet) => { const capitalisedPet = pet.toUpperCase(); - if (petCount[capitalisedPet]) { - petCount[capitalisedPet] += 1; - } else { - petCount[capitalisedPet] = 1; - } + petCount[capitalisedPet] = (petCount[capitalisedPet] || 0) + 1; }); - return petCount; + + return petCount ; } -const countedPetsStartingWithH = countAndCapitalisePets(petsStartingWithH); -console.log(countedPetsStartingWithH); // { 'HAMSTER': 3, 'HORSE': 1 } <- Final console log +console.log(countPets(petsStartingWithH)); // { 'HAMSTER': 3, 'HORSE': 1 } <- Final console log From e3208ffbcbd67125f178dd70aa51d96e89a9f9a7 Mon Sep 17 00:00:00 2001 From: "Karla G." Date: Fri, 27 Feb 2026 00:24:51 +0000 Subject: [PATCH 3/3] docs: document refactoring steps and remove dead code --- Sprint-3/3-dead-code/exercise-2.js | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/Sprint-3/3-dead-code/exercise-2.js b/Sprint-3/3-dead-code/exercise-2.js index 1eb7dc57f..9e73ed48c 100644 --- a/Sprint-3/3-dead-code/exercise-2.js +++ b/Sprint-3/3-dead-code/exercise-2.js @@ -27,8 +27,26 @@ // console.log(countedPetsStartingWithH); // { 'HAMSTER': 3, 'HORSE': 1 } <- Final console log +/** +Removed Unused Global Variables: Deleted const capitalisedPets because the array was being processed +but never used anywhere in the script. +Eliminated Dead Code (Functions): Removed the logPets function since it was never called, keeping +the codebase focused only on the required output. +Simplified Internal Logic: + +Renamed the function to countPets for brevity. + +Replaced the if/else statement inside the forEach loop with a Logical OR shortcut (|| 0). This +handles both initializing the count and incrementing it in a single line. + +Optimized Memory Usage: Removed the intermediate variable countedPetsStartingWithH. Instead, +I passed the function call directly into console.log, as the result didn't need to be stored for further use. + +Improved Code Scope: Ensured the function uses its local parameter instead of relying on external +global variables. +*/ const pets = ["parrot", "hamster", "horse", "dog", "hamster", "cat", "hamster"]; const petsStartingWithH = pets.filter((pet) => pet[0] === "h");