Skip to content

Commit 752f7ae

Browse files
committed
changed the function countChar to use split method, added a more tests to count test file
1 parent 83c98dc commit 752f7ae

File tree

2 files changed

+5
-8
lines changed

2 files changed

+5
-8
lines changed

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

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,7 @@
11
function countChar(stringOfCharacters, findCharacter) {
2-
let pattern = new RegExp(findCharacter, "g");
2+
const totalChar = stringOfCharacters.split(findCharacter);
33

4-
let matchingChars = stringOfCharacters.match(pattern);
5-
6-
if (matchingChars === null) {
7-
return 0;
8-
}
9-
10-
return matchingChars.length;
4+
return totalChar.length - 1;
115
}
126

137
module.exports = countChar;

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ test("should count multiple occurrences of a character", () => {
1515
const char = "a";
1616
const count = countChar(str, char);
1717
expect(count).toEqual(5);
18+
expect(countChar("banana", "n")).toEqual(2);
19+
expect(countChar("=^.^=", "^")).toEqual(2);
20+
expect(countChar("=^.^=", ".")).toEqual(1);
1821
});
1922

2023
// Scenario: No Occurrences

0 commit comments

Comments
 (0)