From bc39a33dd882bc2149cb045104a35ec2c9bb9770 Mon Sep 17 00:00:00 2001 From: Khaliun Baatarkhuu Date: Mon, 20 Jul 2026 15:50:36 +0100 Subject: [PATCH 1/2] 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/2] 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);