diff --git a/Sprint-3/2-practice-tdd/count.js b/Sprint-3/2-practice-tdd/count.js index 95b6ebb7d..3d1adb736 100644 --- a/Sprint-3/2-practice-tdd/count.js +++ b/Sprint-3/2-practice-tdd/count.js @@ -1,5 +1,13 @@ function countChar(stringOfCharacters, findCharacter) { - return 5 + //count the occurrences of the char in the string + // Make it case insensitive + let countChar = 0; + for (const char of stringOfCharacters) { + if (char.toLowerCase() === findCharacter.toLowerCase()) { + countChar++; + } + } + return countChar; } module.exports = countChar; diff --git a/Sprint-3/2-practice-tdd/count.test.js b/Sprint-3/2-practice-tdd/count.test.js index 179ea0ddf..22b5f4ce7 100644 --- a/Sprint-3/2-practice-tdd/count.test.js +++ b/Sprint-3/2-practice-tdd/count.test.js @@ -22,3 +22,38 @@ test("should count multiple occurrences of a character", () => { // And a character `char` that does not exist within `str`. // When the function is called with these inputs, // Then it should return 0, indicating that no occurrences of `char` were found. + +test("should return 0 if the character in not in the given string", () => { + const str = "aaaaa"; + const char = "b"; + const count = countChar(str, char); + expect(count).toEqual(0); +}); + +test("should return 1 if the is 1 occurrence of the char in the string", () => { + const str = "abc"; + const char = "c"; + const count = countChar(str, char); + expect(count).toEqual(1); +}); + +test("should return 0 if there is a empty string ", () => { + const str = ""; + const char = "c"; + const count = countChar(str, char); + expect(count).toEqual(0); +}); + +test("should return 0 if character is empty", () => { + const str = "abcdefg"; + const char = ""; + const count = countChar(str, char); + expect(count).toEqual(0); +}); + +test("should be case insensitive ", () => { + const str = "Successful"; + const char = "s"; + const count = countChar(str, char); + expect(count).toEqual(3); +});