From bc39a33dd882bc2149cb045104a35ec2c9bb9770 Mon Sep 17 00:00:00 2001 From: Khaliun Baatarkhuu Date: Mon, 20 Jul 2026 15:50:36 +0100 Subject: [PATCH 1/4] Refactor sayHello function to eliminate dead code Removed redundant greeting string construction and unreachable console log. --- Sprint-3/3-dead-code/exercise-1.js | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/Sprint-3/3-dead-code/exercise-1.js b/Sprint-3/3-dead-code/exercise-1.js index 4d09f15fa9..ff1fcdb7b1 100644 --- a/Sprint-3/3-dead-code/exercise-1.js +++ b/Sprint-3/3-dead-code/exercise-1.js @@ -15,3 +15,23 @@ testName = "Aman"; const greetingMessage = sayHello(greeting, testName); console.log(greetingMessage); // 'hello, Aman!' + +//I have removed "const greetingStr = greeting + ", " + name + "!";" because it serves the same purpose as "return `${greeting}, ${name}!`;", +//making it redundant. + +//Also, "console.log(greetingStr);" comes after "return `${greeting}, ${name}!`;", making it unreachable inside the function, since eveything +//after "return" is ignored. + + +let testName = "Jerry"; +const greeting = "hello"; + +function sayHello(greeting, name) { + return `${greeting}, ${name}!`; +} + +testName = "Khaliun"; + +const greetingMessage = sayHello(greeting, testName); + +console.log(greetingMessage); // "hello, Khaliun!" From b4cbc57413bace6db9b1e1b264a8fdf9a7aa7c33 Mon Sep 17 00:00:00 2001 From: Khaliun Baatarkhuu Date: Mon, 20 Jul 2026 17:50:47 +0100 Subject: [PATCH 2/4] Clean up unused code in exercise-2.js Removed unused variables and functions to clean up the code. --- Sprint-3/3-dead-code/exercise-2.js | 38 ++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/Sprint-3/3-dead-code/exercise-2.js b/Sprint-3/3-dead-code/exercise-2.js index 56d7887c4c..bdd47c3feb 100644 --- a/Sprint-3/3-dead-code/exercise-2.js +++ b/Sprint-3/3-dead-code/exercise-2.js @@ -1,16 +1,25 @@ // 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. +//let's start from top to bottom and logically think through each line + const pets = ["parrot", "hamster", "horse", "dog", "hamster", "cat", "hamster"]; +//above line is needed because it is used by everything else. + const capitalisedPets = pets.map((pet) => pet.toUpperCase()); +// i am removing above line as "capitalisedPets" is not used anywhere else. + const petsStartingWithH = pets.filter((pet) => pet[0] === "h"); +//this line is filters the words starting with "h" and is used later so we keep it. function logPets(petsArr) { petsArr.forEach((pet) => console.log(pet)); } +//i am erasing the above function as "logPets" is not used anywhere else. function countAndCapitalisePets(petsArr) { const petCount = {}; +//keeping this function, as petCount is used below. petsArr.forEach((pet) => { const capitalisedPet = pet.toUpperCase(); @@ -22,7 +31,36 @@ function countAndCapitalisePets(petsArr) { }); return petCount; } +//everything above is needed to count how many of each pets there are. const countedPetsStartingWithH = countAndCapitalisePets(petsStartingWithH); +//it is used for function call, so needed. console.log(countedPetsStartingWithH); // { 'HAMSTER': 3, 'HORSE': 1 } <- Final console log +//we wanna see the result of our machinations so we keep it. + +//THE FOLLOWING CODE IS THE CLEANED UP VERSION. + +const pets = ["parrot", "hamster", "horse", "dog", "hamster", "cat", "hamster"]; + +const petsStartingWithH = pets.filter((pet) => pet[0] === "h"); + +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); From 822fa7a8e83db5c7e85f65eaa8b27c2945001cae Mon Sep 17 00:00:00 2001 From: Khaliun Baatarkhuu Date: Sat, 25 Jul 2026 16:57:31 +0100 Subject: [PATCH 3/4] Clean up sayHello function and fix logging Removed unreachable code and redundant variable. --- Sprint-3/3-dead-code/exercise-1.js | 26 +------------------------- 1 file changed, 1 insertion(+), 25 deletions(-) diff --git a/Sprint-3/3-dead-code/exercise-1.js b/Sprint-3/3-dead-code/exercise-1.js index ff1fcdb7b1..7544506d0f 100644 --- a/Sprint-3/3-dead-code/exercise-1.js +++ b/Sprint-3/3-dead-code/exercise-1.js @@ -1,27 +1,3 @@ -// 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"; -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!' - -//I have removed "const greetingStr = greeting + ", " + name + "!";" because it serves the same purpose as "return `${greeting}, ${name}!`;", -//making it redundant. - -//Also, "console.log(greetingStr);" comes after "return `${greeting}, ${name}!`;", making it unreachable inside the function, since eveything -//after "return" is ignored. - let testName = "Jerry"; const greeting = "hello"; @@ -34,4 +10,4 @@ testName = "Khaliun"; const greetingMessage = sayHello(greeting, testName); -console.log(greetingMessage); // "hello, Khaliun!" +console.log(greetingMessage); From e09b771d641af7197f800ecf9f92b65c9c2394f2 Mon Sep 17 00:00:00 2001 From: Khaliun Baatarkhuu Date: Sat, 25 Jul 2026 17:00:15 +0100 Subject: [PATCH 4/4] Clean up dead code in exercise-2.js Removed unused variables and functions to clean up the code. --- Sprint-3/3-dead-code/exercise-2.js | 43 ------------------------------ 1 file changed, 43 deletions(-) diff --git a/Sprint-3/3-dead-code/exercise-2.js b/Sprint-3/3-dead-code/exercise-2.js index bdd47c3feb..0f211fa19d 100644 --- a/Sprint-3/3-dead-code/exercise-2.js +++ b/Sprint-3/3-dead-code/exercise-2.js @@ -1,46 +1,3 @@ -// 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. - -//let's start from top to bottom and logically think through each line - -const pets = ["parrot", "hamster", "horse", "dog", "hamster", "cat", "hamster"]; -//above line is needed because it is used by everything else. - -const capitalisedPets = pets.map((pet) => pet.toUpperCase()); -// i am removing above line as "capitalisedPets" is not used anywhere else. - -const petsStartingWithH = pets.filter((pet) => pet[0] === "h"); -//this line is filters the words starting with "h" and is used later so we keep it. - -function logPets(petsArr) { - petsArr.forEach((pet) => console.log(pet)); -} -//i am erasing the above function as "logPets" is not used anywhere else. - -function countAndCapitalisePets(petsArr) { - const petCount = {}; -//keeping this function, as petCount is used below. - - petsArr.forEach((pet) => { - const capitalisedPet = pet.toUpperCase(); - if (petCount[capitalisedPet]) { - petCount[capitalisedPet] += 1; - } else { - petCount[capitalisedPet] = 1; - } - }); - return petCount; -} -//everything above is needed to count how many of each pets there are. - -const countedPetsStartingWithH = countAndCapitalisePets(petsStartingWithH); -//it is used for function call, so needed. - -console.log(countedPetsStartingWithH); // { 'HAMSTER': 3, 'HORSE': 1 } <- Final console log -//we wanna see the result of our machinations so we keep it. - -//THE FOLLOWING CODE IS THE CLEANED UP VERSION. - const pets = ["parrot", "hamster", "horse", "dog", "hamster", "cat", "hamster"]; const petsStartingWithH = pets.filter((pet) => pet[0] === "h");