Skip to content

Commit c518625

Browse files
committed
added tests for multiple occurences and for no occurances then made the function that checks for that
1 parent 8f3d6cf commit c518625

File tree

4 files changed

+4445
-3
lines changed

4 files changed

+4445
-3
lines changed

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 pattern = new RegExp(findCharacter, "g");
3+
4+
let matchingChars = stringOfCharacters.match(pattern);
5+
6+
if (matchingChars === null) {
7+
return 0;
8+
}
9+
10+
return matchingChars.length;
311
}
412

513
module.exports = countChar;

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

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,19 @@ const countChar = require("./count");
1212

1313
test("should count multiple occurrences of a character", () => {
1414
const str = "aaaaa";
15-
const char = "a";
15+
const char = "b";
1616
const count = countChar(str, char);
17-
expect(count).toEqual(5);
17+
expect(count).toEqual(3);
1818
});
1919

2020
// Scenario: No Occurrences
2121
// Given the input string str,
2222
// And a character char that does not exist within the case-sensitive str,
2323
// When the function is called with these inputs,
2424
// Then it should return 0, indicating that no occurrences of the char were found in the case-sensitive str.
25+
test("should check for no occurrences of a character", () => {
26+
const str = "abcdef";
27+
const char = "g";
28+
const count = countChar(str, char);
29+
expect(count).toEqual(0);
30+
});

0 commit comments

Comments
 (0)