Skip to content

Commit 8d018fd

Browse files
committed
Implement countChar and add Jest tests
1 parent b31a586 commit 8d018fd

2 files changed

Lines changed: 36 additions & 3 deletions

File tree

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
function countChar(stringOfCharacters, findCharacter) {
2-
return 5
2+
let count = 0;
3+
for (const c of stringOfCharacters) {
4+
if (c === findCharacter) count++;
5+
}
6+
return count;
37
}
4-
58
module.exports = countChar;

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

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,41 @@ const countChar = require("./count");
1010
// When the function is called with these inputs,
1111
// Then it should correctly count occurrences of `char`.
1212

13-
test("should count multiple occurrences of a character", () => {
13+
/*test("should count multiple occurrences of a character", () => {
1414
const str = "aaaaa";
1515
const char = "a";
1616
const count = countChar(str, char);
1717
expect(count).toEqual(5);
18+
});*/
19+
20+
describe("countChar", () => {
21+
test("counts repeated characters in a simple string", () => {
22+
expect(countChar("aaaaa", "a")).toEqual(5);
23+
});
24+
25+
test("counts characters in a mixed string", () => {
26+
expect(countChar("absankama", "a")).toEqual(4);
27+
});
28+
29+
test("counts characters including spaces", () => {
30+
expect(countChar("ab san kama", "a")).toEqual(4);
31+
});
32+
33+
test("counts characters in string with symbols", () => {
34+
expect(countChar("a-+&£$%?/|b san kama", "a")).toEqual(4);
35+
});
36+
37+
test("returns 0 when character is not found", () => {
38+
expect(countChar("-+&£$%?/|b sn km", "a")).toEqual(0);
39+
});
40+
41+
test("returns 0 for empty string", () => {
42+
expect(countChar("", "a")).toEqual(0);
43+
});
44+
45+
test("is case sensitive", () => {
46+
expect(countChar("AaAa", "a")).toEqual(2);
47+
});
1848
});
1949

2050
// Scenario: No Occurrences

0 commit comments

Comments
 (0)