Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 31 additions & 8 deletions Sprint-3/3-dead-code/exercise-1.js
Original file line number Diff line number Diff line change
@@ -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!'
68 changes: 53 additions & 15 deletions Sprint-3/3-dead-code/exercise-2.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,66 @@
// 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

/**
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 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