Skip to content

Commit fe5eec7

Browse files
Add exercises for dead-code
1 parent d074c28 commit fe5eec7

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// Find the instances of unreachable and redundant code - remove them!
2+
// The sayHello function should continue to work for any reasonable input it's given.
3+
4+
const greeting = "hello";
5+
6+
function sayHello(greeting, name) {
7+
return `${greeting}, ${name}!`;
8+
}
9+
10+
const testName = "Aman";
11+
12+
const greetingMessage = sayHello(greeting, testName); // only needed if want to store variable
13+
14+
console.log(greetingMessage); // 'hello, Aman!'

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

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

0 commit comments

Comments
 (0)