Skip to content

Commit e947f0f

Browse files
implement count function and wrote diffrent test cases.
1 parent e09e648 commit e947f0f

2 files changed

Lines changed: 47 additions & 2 deletions

File tree

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

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,14 @@
11
function countChar(stringOfCharacters, findCharacter) {
2-
return 5
3-
}
2+
if (typeof stringOfCharacters !== "string") return 0;
3+
4+
let count = 0;
45

6+
for (let char of stringOfCharacters) {
7+
if (char === findCharacter) {
8+
count++;
9+
}
10+
}
11+
12+
return count;
13+
}
514
module.exports = countChar;

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

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,39 @@ test("should count multiple occurrences of a character", () => {
2222
// And a character `char` that does not exist within `str`.
2323
// When the function is called with these inputs,
2424
// Then it should return 0, indicating that no occurrences of `char` were found.
25+
test("should return 0 when character does not exist in string", () => {
26+
const str = "hello";
27+
const char = "z";
28+
const count = countChar(str, char);
29+
expect(count).toEqual(0);
30+
});
31+
32+
// Scenario: Case Sensitivity
33+
test("should be case sensitive", () => {
34+
expect(countChar("AaAa", "a")).toBe(2);
35+
});
36+
37+
// Scenario: Empty String
38+
test("should return 0 for an empty string", () => {
39+
expect(countChar("", "a")).toBe(0);
40+
});
41+
42+
// Scenario: Special Characters
43+
test("should count special characters", () => {
44+
expect(countChar("!@#$%^&*()!", "!")).toBe(2);
45+
});
46+
47+
// Scenario: Non-String Inputs
48+
test("should handle non-string inputs gracefully", () => {
49+
expect(countChar(12345, "1")).toBe(0);
50+
expect(countChar(null, "a")).toBe(0);
51+
expect(countChar(undefined, "a")).toBe(0);
52+
});
53+
test ("should count numeric characters in a string", () => {
54+
55+
expect(countChar("12345", "1")).toBe(1);
56+
});
57+
// Scenario: Space Character
58+
test("should count spaces correctly", () => {
59+
expect(countChar("a a a", " ")).toBe(2);
60+
});

0 commit comments

Comments
 (0)