Skip to content

Commit bef7de6

Browse files
committed
Implement countChar function and add tests for character occurrences
1 parent 3175f13 commit bef7de6

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed

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 char of stringOfCharacters){
4+
if (char === findCharacter) {
5+
count++;
6+
}
7+
}
8+
return count;
9+
}
410

511
module.exports = countChar;

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,12 @@ test("should count multiple occurrences of a character", () => {
1616
const count = countChar(str, char);
1717
expect(count).toEqual(5);
1818
});
19+
test ("should return 0 when the character does not exist in the string", () => {
20+
const str = "exist";
21+
const char = "z";
22+
const count = countChar(str, char);
23+
expect(count).toEqual(0);
24+
});
1925

2026
// Scenario: No Occurrences
2127
// Given the input string `str`,

0 commit comments

Comments
 (0)