diff --git a/dead-code/exercise-1.js b/dead-code/exercise-1.js index 4d09f15fa9..209fadc9b8 100644 --- a/dead-code/exercise-1.js +++ b/dead-code/exercise-1.js @@ -1,17 +1,13 @@ // 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"; +let testName = "Aman"; 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/dead-code/exercise-2.js b/dead-code/exercise-2.js index 56d7887c4c..1d292f2f8c 100644 --- a/dead-code/exercise-2.js +++ b/dead-code/exercise-2.js @@ -2,12 +2,8 @@ // 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)); -} +const petsStartingWithH = pets.filter((pet) => pet[0] === "h"); function countAndCapitalisePets(petsArr) { const petCount = {}; diff --git a/practice-tdd/count.js b/practice-tdd/count.js index 5518c57123..0b461dfd4b 100644 --- a/practice-tdd/count.js +++ b/practice-tdd/count.js @@ -1,3 +1,10 @@ export function countChar(stringOfCharacters, findCharacter) { - return 5 + let count = 0; + + for (const character of stringOfCharacters) { + if (character === findCharacter) { + count++; + } + } + return count; } diff --git a/practice-tdd/count.test.js b/practice-tdd/count.test.js index cb3aaae591..b9c70b3f68 100644 --- a/practice-tdd/count.test.js +++ b/practice-tdd/count.test.js @@ -17,6 +17,21 @@ test("should count multiple occurrences of a character", () => { expect(count).toEqual(5); }); +test("should count occurrences in a mixed string", () => { + const count = countChar("banana", "a"); + expect(count).toEqual(3); +}); + +test("should return 0 for an empty string", () => { + const count = countChar("", "a"); + expect(count).toEqual(0); +}); + +test("should return 0 when character does not occur", () => { + const count = countChar("hello", "z"); + expect(count).toEqual(0); +}); + // Scenario: No Occurrences // Given the input string `str`, // And a character `char` that does not exist within `str`.