diff --git a/Sprint-3/dead-code/README.md b/Sprint-3/dead-code/README.md new file mode 100644 index 00000000..785101d3 --- /dev/null +++ b/Sprint-3/dead-code/README.md @@ -0,0 +1,9 @@ +# Refactoring Dead Code + +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. + +## Instructions + +1. Work through each `exercise` file inside this directory. +2. Delete the dead code. +3. Commit your changes and make a PR when done. diff --git a/Sprint-3/dead-code/exercise-1.js b/Sprint-3/dead-code/exercise-1.js new file mode 100644 index 00000000..6077b36f --- /dev/null +++ b/Sprint-3/dead-code/exercise-1.js @@ -0,0 +1,16 @@ +// Find the instances of unreachable and redundant code - remove them! + +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!' diff --git a/Sprint-3/dead-code/exercise-2.js b/Sprint-3/dead-code/exercise-2.js new file mode 100644 index 00000000..df0e3b45 --- /dev/null +++ b/Sprint-3/dead-code/exercise-2.js @@ -0,0 +1,27 @@ +// Remove the unused code that does not contribute to the final console log + +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