Skip to content

Commit 3bd121a

Browse files
committed
adding condition checks in count.js and edge cases in the test
1 parent 4792575 commit 3bd121a

File tree

2 files changed

+37
-3
lines changed

2 files changed

+37
-3
lines changed

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

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,15 @@
11
function countChar(stringOfCharacters, findCharacter) {
2-
const arrayOfCharacters = stringOfCharacters.split("");
2+
let arrayOfCharacters = [];
3+
if (typeof stringOfCharacters === "string") {
4+
arrayOfCharacters = stringOfCharacters.split("");
5+
} else if (Array.isArray(stringOfCharacters)) {
6+
arrayOfCharacters = stringOfCharacters;
7+
} else if (typeof stringOfCharacters === "number") {
8+
arrayOfCharacters = stringOfCharacters.toString().split("");
9+
} else if (typeof stringOfCharacters === "object") {
10+
return 0;
11+
}
12+
313
let count = 0;
414
for (let index = 0; index < arrayOfCharacters.length; index++) {
515
if (arrayOfCharacters[index] === findCharacter) {

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

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ test("should count multiple occurrences of a character", () => {
1414
const str = "aaaaa";
1515
const char = "a";
1616
const count = countChar(str, char);
17-
expect(count).toEqual(5);
17+
expect(parseInt(count)).toEqual(5);
1818
});
1919

2020
// Scenario: No Occurrences
@@ -27,5 +27,29 @@ test("should return 0 when character does not exist in the string", () => {
2727
const str = "abcdefg";
2828
const char = "h";
2929
const count = countChar(str, char);
30-
expect(count).toEqual(0);
30+
expect(parseInt(count)).toEqual(0);
31+
});
32+
33+
// test for empty string
34+
test("should return 0 when string is empty", () => {
35+
const str = "";
36+
const char = "a";
37+
const count = countChar(str, char);
38+
expect(parseInt(count)).toEqual(0);
39+
});
40+
41+
// test for str is an array
42+
test("should return 0 when str is an array", () => {
43+
const str = ["a", "b", "c"];
44+
const char = "a";
45+
const count = countChar(str, char);
46+
expect(parseInt(count)).toEqual(1);
47+
});
48+
49+
// test for str is a number
50+
test("should return 0 when str is a number", () => {
51+
const str = 12345;
52+
const char = "3";
53+
const count = countChar(str, char);
54+
expect(parseInt(count)).toEqual(1);
3155
});

0 commit comments

Comments
 (0)