Skip to content

Commit 2958939

Browse files
committed
updated count char function and tests to include edge/corner cases and invalid inputs
1 parent 8a60a16 commit 2958939

File tree

2 files changed

+65
-4
lines changed

2 files changed

+65
-4
lines changed

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

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,19 @@
11
function countChar(stringOfCharacters, findCharacter) {
2-
return stringOfCharacters.split("").reduce((count, char) => {
3-
if (char === findCharacter) count++;
4-
return count;
5-
}, 0);
2+
if (typeof stringOfCharacters !== 'string'){
3+
throw new Error("First argument must be a string.");
4+
}
5+
if (typeof findCharacter !== "string") {
6+
throw new Error("Second argument must be a string.");
7+
}
8+
if (findCharacter.length !== 1) {
9+
throw new Error("Character to find must be a single character.");
10+
}
11+
if (stringOfCharacters.length === 0) {
12+
return 0;
13+
}
14+
console.log(Array.from(stringOfCharacters));
15+
16+
return Array.from(stringOfCharacters).filter(char => char === findCharacter).length;
617
}
718

819
module.exports = countChar;

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

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,3 +53,53 @@ test("should return 0 when the input string is empty", () => {
5353
expect(count).toEqual(0);
5454
});
5555

56+
// Scenario: Numeric Characters
57+
test("should count numeric characters in the string", () => {
58+
const str = "123123123";
59+
const char = "2";
60+
const count = countChar(str, char);
61+
expect(count).toEqual(3);
62+
});
63+
64+
// Scenario: Whitespace Characters
65+
test("should count whitespace characters in the string", () => {
66+
const str = "a b c d e f ";
67+
const char = " ";
68+
const count = countChar(str, char);
69+
expect(count).toEqual(6);
70+
});
71+
72+
// Scenario: Edge Case - Character Not a Single Character
73+
test('should throw error if char is not a single character', () => {
74+
const str = 'hello';
75+
const char = 'll';
76+
expect(() => {
77+
countChar(str, char);
78+
}).toThrow('Character to find must be a single character');
79+
})
80+
81+
// Scenario: Special Case - Special Characters
82+
test.each([
83+
{ str: "!!!@@@###", char: "!", expected: 3 },
84+
{ str: "$$$%%%^^^", char: "%", expected: 3 },
85+
{ str: "&&&***(((", char: "(", expected: 3 },
86+
])(
87+
"should count special characters correctly in '$str'",
88+
({ str, char, expected }) => {
89+
const count = countChar(str, char);
90+
expect(count).toEqual(expected);
91+
}
92+
);
93+
//Scenario: Invalid Input Types
94+
test.each([
95+
{ str: 12345, char: "a", error: "First argument must be a string." },
96+
{ str: "hello", char: 5, error: "Second argument must be a string." },
97+
{ str: [], char: "a", error: "First argument must be a string." },
98+
{ str: "hello", char: {}, error: "Second argument must be a string." },
99+
{ str: null, char: "a", error: "First argument must be a string." },
100+
{str: "hi", char: undefined, error: "Second argument must be a string." },
101+
])("should throw error for invalid inputs", ({ str, char, error }) => {
102+
expect(() => {
103+
countChar(str, char);
104+
}).toThrow(error);
105+
});

0 commit comments

Comments
 (0)