Skip to content

Commit babb7ac

Browse files
committed
modified countChar function so it can work for the non-alphabet test cases
1 parent c456e28 commit babb7ac

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
function countChar(stringOfCharacters, findCharacter) {
2-
return stringOfCharacters.split(findCharacter).length - 1;
2+
if (findCharacter == "") return 0;
3+
4+
const string = stringOfCharacters.toLowerCase();
5+
const char = findCharacter.toLowerCase();
6+
7+
return string.split(char).length - 1;
38
}
49

510
module.exports = countChar;

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,19 @@ test(`should return 0 for no occurances of 'char' in 'str'`, () => {
2828
const count = countChar(str, char);
2929
expect(count).toEqual(0);
3030
})
31+
32+
test(`in case sensitive case, should also return the number of character in the string`, () => {
33+
expect(countChar("Code Your Future!", "Y")).toEqual(1);
34+
});
35+
36+
test(`should return the count of character for non-alphabests`, () => {
37+
expect(countChar("86421", "1")).toEqual(1);
38+
});
39+
40+
test(`should return 0 for empty strings`, () => {
41+
expect(countChar("code your future", "")).toEqual(0)
42+
});
43+
44+
test(`should return the count for unusual characters`, () => {
45+
expect(countChar("code& $future £your", "$")).toEqual(1)
46+
})

0 commit comments

Comments
 (0)