Skip to content

Commit e108f82

Browse files
Implement countChar function and add tests for character occurrences
1 parent 29ef74a commit e108f82

2 files changed

Lines changed: 20 additions & 1 deletion

File tree

‎practice-tdd/count.js‎

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

‎practice-tdd/count.test.js‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,20 @@ test("should count multiple occurrences of a character", () => {
1717
expect(count).toEqual(5);
1818
});
1919

20+
test("should return 0 when character is not found", () => {
21+
const str = "hello";
22+
const char = "z";
23+
const count = countChar(str, char);
24+
expect(count).toEqual(0);
25+
});
26+
27+
test("should count 2 occurrences of n", () => {
28+
const str = "banana";
29+
const char = "n";
30+
const count = countChar(str, char);
31+
expect(count).toEqual(2);
32+
});
33+
2034
// Scenario: No Occurrences
2135
// Given the input string `str`,
2236
// And a character `char` that does not exist within `str`.

0 commit comments

Comments
 (0)