Skip to content

Commit 2cd8d18

Browse files
committed
implement countChar function and associated tests
1 parent 8f3d6cf commit 2cd8d18

File tree

2 files changed

+35
-1
lines changed

2 files changed

+35
-1
lines changed

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
function countChar(stringOfCharacters, findCharacter) {
2-
return 5
2+
return stringOfCharacters.split("").reduce((count, char) => {
3+
if (char === findCharacter) count++;
4+
return count;
5+
}, 0);
36
}
47

58
module.exports = countChar;

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

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,34 @@ test("should count multiple occurrences of a character", () => {
2222
// And a character char that does not exist within the case-sensitive str,
2323
// When the function is called with these inputs,
2424
// Then it should return 0, indicating that no occurrences of the char were found in the case-sensitive str.
25+
test("should return 0 if no occurrences of a character", () => {
26+
const str = "bbbbbb";
27+
const char = "a";
28+
const count = countChar(str, char);
29+
expect(count).toEqual(0);
30+
});
31+
32+
// Scenario: Single Occurrence
33+
test("should return 1 for a single occurrence of a character", () => {
34+
const str = "hello";
35+
const char = "e";
36+
const count = countChar(str, char);
37+
expect(count).toEqual(1);
38+
});
39+
40+
// Scenario: Case Sensitivity
41+
test("should be case sensitive when counting characters", () => {
42+
const str = "Hello World";
43+
const char = "h";
44+
const count = countChar(str, char);
45+
expect(count).toEqual(0);
46+
});
47+
48+
// Scenario: Empty String
49+
test("should return 0 when the input string is empty", () => {
50+
const str = "";
51+
const char = "a";
52+
const count = countChar(str, char);
53+
expect(count).toEqual(0);
54+
});
55+

0 commit comments

Comments
 (0)