Skip to content

Commit 96408b3

Browse files
Sprint 3 task 2 exercise count.js completed
1 parent 8934909 commit 96408b3

2 files changed

Lines changed: 39 additions & 1 deletion

File tree

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+
const regEx = new RegExp(`[^${findCharacter}]`, "g");
3+
const numOfMatchedChar = stringOfCharacters.replaceAll(regEx, "").length;
4+
return numOfMatchedChar;
35
}
46

7+
console.log(countChar("dafsadf", "a"));
8+
59
module.exports = countChar;

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

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,37 @@ 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 no occurrence of 'char' is found in 'str'", () => {
26+
const str = "cde";
27+
const char = "a";
28+
const count = countChar(str, char);
29+
expect(count).toEqual(0);
30+
});
31+
32+
// Scenario: Empty string
33+
// Given an empty string `str`
34+
// And a single character `char`
35+
// When countChar is called
36+
// Then it should return 0
37+
test("should return 0 when the string is empty", () => {
38+
const str = "";
39+
const char = "a";
40+
41+
const count = countChar(str, char);
42+
43+
expect(count).toEqual(0);
44+
});
45+
46+
// Scenario: One Occurrence
47+
// Given a string `str`
48+
// And a single character `char` that occurs once in `str`
49+
// When countChar is called
50+
// Then it should return 1
51+
test("should count one occurrence of a character", () => {
52+
const str = "abcde";
53+
const char = "a";
54+
55+
const count = countChar(str, char);
56+
57+
expect(count).toEqual(1);
58+
});

0 commit comments

Comments
 (0)