Skip to content

Commit d65f688

Browse files
Implement countChar function to count character occurrences and add test for zero occurrences
1 parent 793cbac commit d65f688

2 files changed

Lines changed: 12 additions & 2 deletions

File tree

Sprint-3/2-practice-tdd/count.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
function countChar(stringOfCharacters, findCharacter) {
2-
return 5
3-
}
2+
let count = 0;
3+
for (let i = 0; i < stringOfCharacters.length; i++) {
4+
if (stringOfCharacters[i] === findCharacter) {
5+
count++;
6+
}
7+
}
48

9+
return count;
10+
}
511
module.exports = countChar;

Sprint-3/2-practice-tdd/count.test.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ test("should count multiple occurrences of a character", () => {
1818
});
1919

2020
// Scenario: No Occurrences
21+
test("should return 0 when the character does not occur in the string", () => {
22+
const count = countChar("hello", "z");
23+
expect(count).toEqual(0);
24+
});
2125
// Given the input string `str`,
2226
// And a character `char` that does not exist within `str`.
2327
// When the function is called with these inputs,

0 commit comments

Comments
 (0)