Skip to content

Commit a57e153

Browse files
committed
Implement countChar with tests
1 parent b31a586 commit a57e153

2 files changed

Lines changed: 22 additions & 9 deletions

File tree

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

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
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) {
5+
count++;
6+
}
7+
}
8+
return count;
39
}
410

5-
module.exports = countChar;
11+
module.exports = countChar;
Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
// implement a function countChar that counts the number of times a character occurs in a string
22
const countChar = require("./count");
3-
// Given a string `str` and a single character `char` to search for,
3+
// Given a string str and a single character char to search for,
44
// When the countChar function is called with these inputs,
55
// Then it should:
66

77
// Scenario: Multiple Occurrences
8-
// Given the input string `str`,
9-
// And a character `char` that occurs one or more times in `str` (e.g., 'a' in 'aaaaa'),
8+
// Given the input string str,
9+
// And a character char that occurs one or more times in str (e.g., 'a' in 'aaaaa'),
1010
// When the function is called with these inputs,
11-
// Then it should correctly count occurrences of `char`.
11+
// Then it should correctly count occurrences of char.
1212

1313
test("should count multiple occurrences of a character", () => {
1414
const str = "aaaaa";
@@ -18,7 +18,14 @@ test("should count multiple occurrences of a character", () => {
1818
});
1919

2020
// Scenario: No Occurrences
21-
// Given the input string `str`,
22-
// And a character `char` that does not exist within `str`.
21+
// Given the input string str,
22+
// And a character char that does not exist within str.
2323
// When the function is called with these inputs,
24-
// Then it should return 0, indicating that no occurrences of `char` were found.
24+
// Then it should return 0, indicating that no occurrences of char were found.
25+
26+
test("should return 0 when the character does not occur", () => {
27+
const str = "aaaaa";
28+
const char = "b";
29+
const count = countChar(str, char);
30+
expect(count).toEqual(0);
31+
});

0 commit comments

Comments
 (0)