Skip to content

Commit c89c066

Browse files
authored
Merge pull request #332 from CodeYourFuture/praj-deadcode-backlog
Dead Code Backlog Exercises
2 parents 282246f + 052c3a9 commit c89c066

File tree

3 files changed

+52
-0
lines changed

3 files changed

+52
-0
lines changed

Sprint-3/dead-code/README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Refactoring Dead Code
2+
3+
Here are two example of code that has not been built efficiently. Both files have dead code in them, it's your job to go back through this existing code, identify the dead code, and remove it so the code is ready for production.
4+
5+
## Instructions
6+
7+
1. Work through each `exercise` file inside this directory.
8+
2. Delete the dead code.
9+
3. Commit your changes and make a PR when done.

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Find the instances of unreachable and redundant code - remove them!
2+
3+
let testName = "Jerry";
4+
const greeting = "hello";
5+
6+
function sayHello(greeting, name) {
7+
const greetingStr = greeting + ", " + name + "!";
8+
return `${greeting}, ${name}!`;
9+
console.log(greetingStr);
10+
}
11+
12+
testName = "Aman";
13+
14+
const greetingMessage = sayHello(greeting, testName);
15+
16+
console.log(greetingMessage); // 'hello, Aman!'

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

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// Remove the unused code that does not contribute to the final console log
2+
3+
const pets = ["parrot", "hamster", "horse", "dog", "hamster", "cat", "hamster"];
4+
const capitalisedPets = pets.map((pet) => pet.toUpperCase());
5+
const petsStartingWithH = pets.filter((pet) => pet[0] === "h");
6+
7+
function logPets(petsArr) {
8+
petsArr.forEach((pet) => console.log(pet));
9+
}
10+
11+
function countAndCapitalisePets(petsArr) {
12+
const petCount = {};
13+
14+
petsArr.forEach((pet) => {
15+
const capitalisedPet = pet.toUpperCase();
16+
if (petCount[capitalisedPet]) {
17+
petCount[capitalisedPet] += 1;
18+
} else {
19+
petCount[capitalisedPet] = 1;
20+
}
21+
});
22+
return petCount;
23+
}
24+
25+
const countedPetsStartingWithH = countAndCapitalisePets(petsStartingWithH);
26+
27+
console.log(countedPetsStartingWithH); // { 'HAMSTER': 3, 'HORSE': 1 } <- Final console log

0 commit comments

Comments
 (0)