Skip to content

Commit 18ba233

Browse files
committed
Complete countChar implementation with Jest test cases
1 parent b31a586 commit 18ba233

2 files changed

Lines changed: 27 additions & 1 deletion

File tree

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
function countChar(stringOfCharacters, findCharacter) {
2-
return 5
2+
let count = 0;
3+
4+
for (let char of stringOfCharacters) {
5+
if (char === findCharacter) {
6+
count++;
7+
}
8+
}
9+
10+
return count;
311
}
412

513
module.exports = countChar;

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,27 @@ 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 for an empty string", () => {
20+
const str = "";
21+
const char = "a";
22+
const count = countChar(str, char);
23+
expect(count).toEqual(0);
24+
});
25+
test("should count 'a' in a sentence with spaces", () => {
26+
const str = "My name is Jane";
27+
const char = "a";
28+
const count = countChar(str, char);
29+
expect(count).toEqual(2);
30+
});
1931

2032
// Scenario: No Occurrences
2133
// Given the input string `str`,
2234
// And a character `char` that does not exist within `str`.
2335
// When the function is called with these inputs,
2436
// Then it should return 0, indicating that no occurrences of `char` were found.
37+
test("should return 0 when character does not occur", () => {
38+
const str = "hello";
39+
const char = "z";
40+
const count = countChar(str, char);
41+
expect(count).toEqual(0);
42+
});

0 commit comments

Comments
 (0)