Skip to content

Commit 966e522

Browse files
committed
add occurences count test and function
1 parent b31a586 commit 966e522

2 files changed

Lines changed: 18 additions & 1 deletion

File tree

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
function countChar(stringOfCharacters, findCharacter) {
2-
return 5
2+
if (!stringOfCharacters.includes(findCharacter)) {
3+
return 0;
4+
}
5+
return stringOfCharacters.split("").filter((char) => char === findCharacter)
6+
.length;
37
}
48

59
module.exports = countChar;

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

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

20+
test("should count multiple occurrences of a different character", () => {
21+
const str = "banana";
22+
const char = "n";
23+
const count = countChar(str, char);
24+
expect(count).toEqual(2);
25+
});
26+
2027
// Scenario: No Occurrences
2128
// Given the input string `str`,
2229
// And a character `char` that does not exist within `str`.
2330
// When the function is called with these inputs,
2431
// Then it should return 0, indicating that no occurrences of `char` were found.
32+
test("should return 0 when no occurrences of a character are found", () => {
33+
const str = "aaaaa";
34+
const char = "b";
35+
const count = countChar(str, char);
36+
expect(count).toEqual(0);
37+
});

0 commit comments

Comments
 (0)