Skip to content

Commit 8d9a2ef

Browse files
committed
feat(sprint-3): implement count function with TDD
1 parent 962adf5 commit 8d9a2ef

File tree

2 files changed

+25
-20
lines changed

2 files changed

+25
-20
lines changed

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+
let count = 0;
3+
for (let i = 0; i < stringOfCharacters.length; i++) {
4+
if (stringOfCharacters[i] === findCharacter) count++;
5+
}
6+
return count;
37
}
48

59
module.exports = countChar;
Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,25 @@
1-
// implement a function countChar that counts the number of times a character occurs in a string
21
const countChar = require("./count");
3-
// Given a string str and a single character char to search for,
4-
// When the countChar function is called with these inputs,
5-
// Then it should:
6-
7-
// Scenario: Multiple Occurrences
8-
// Given the input string str,
9-
// And a character char that may occur multiple times with overlaps within str (e.g., 'a' in 'aaaaa'),
10-
// When the function is called with these inputs,
11-
// Then it should correctly count overlapping occurrences of char (e.g., 'a' appears five times in 'aaaaa').
122

133
test("should count multiple occurrences of a character", () => {
14-
const str = "aaaaa";
15-
const char = "a";
16-
const count = countChar(str, char);
17-
expect(count).toEqual(5);
4+
expect(countChar("aaaaa", "a")).toEqual(5);
5+
});
6+
7+
test("should return 0 when character is not found", () => {
8+
expect(countChar("hello", "z")).toEqual(0);
9+
});
10+
11+
test("should count a single occurrence of a character", () => {
12+
expect(countChar("hello", "h")).toEqual(1);
1813
});
1914

20-
// Scenario: No Occurrences
21-
// Given the input string str,
22-
// And a character char that does not exist within the case-sensitive str,
23-
// When the function is called with these inputs,
24-
// Then it should return 0, indicating that no occurrences of the char were found in the case-sensitive str.
15+
test("should be case-sensitive", () => {
16+
expect(countChar("Hello", "h")).toEqual(0);
17+
});
18+
19+
test("should count uppercase characters separately", () => {
20+
expect(countChar("Hello", "H")).toEqual(1);
21+
});
22+
23+
test("should count character occurring multiple times in different positions", () => {
24+
expect(countChar("banana", "a")).toEqual(3);
25+
});

0 commit comments

Comments
 (0)