Skip to content

Commit 1092e07

Browse files
author
Baba05206
committed
Enhance countChar function with improved error handling and refactor tests for clarity
1 parent bedc468 commit 1092e07

File tree

2 files changed

+45
-109
lines changed

2 files changed

+45
-109
lines changed

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

Lines changed: 8 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -7,36 +7,23 @@ function countChar(stringOfCharacters, findCharacter) {
77
return "Error: The string cannot be empty.";
88
}
99

10-
const chars = [...stringOfCharacters];
11-
const findChars = [...findCharacter];
10+
if (findCharacter == null) {
11+
return "Error: The character to count must be a single character.";
12+
}
1213

13-
if (!findCharacter || findChars.length !== 1) {
14+
const findChars = [...findCharacter];
15+
if (findChars.length !== 1) {
1416
return "Error: The character to count must be a single character.";
1517
}
1618

1719
let count = 0;
18-
for (let ch of chars) {
20+
for (let ch of stringOfCharacters) {
1921
if (ch === findCharacter) count++;
2022
}
2123

2224
return count;
2325
}
2426

25-
// Examples
26-
console.log(countChar("hello", "l")); // Expected: 2
27-
console.log(countChar("javascript", "a")); // Expected: 2
28-
console.log(countChar("hello", "z")); // Expected: 0
29-
console.log(countChar("null", "")); // Expected: "Error: The character to count must be a single character."
30-
console.log(countChar("", "a")); // Expected: "Error: The string cannot be empty."
31-
console.log(countChar("hello", "ll")); // Expected: "Error: The character to count must be a single character."
32-
console.log(countChar("hipopotamos' make wonderful pets", "o")); // Expected: 3
33-
console.log(countChar("hipopotamos", "p")); // Expected: 1
34-
console.log(countChar("hipopotamos' are a friendly animal", "t")); // Expected: 1
35-
console.log(countChar("hipopotamos", "x")); // Expected: 0
36-
console.log(countChar("Pneumonoultramicroscopicsilicovolcanoconiosis", "i")); // Expected: 6
37-
console.log(countChar("null", "")); // Expected: "Error: The character to count must be a single character."
38-
console.log(countChar("", "a")); // Expected: "Error: The string cannot be empty."
39-
console.log(countChar("hello", "ll")); // Expected: "Error: The character to count must be a single character."
40-
console.log(countChar(null, "a")); // Expected: "Error: The input string cannot be null or undefined."
41-
4227
module.exports = countChar;
28+
29+
// implement a function countChar that counts the number of times a character occurs in a string

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

Lines changed: 37 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -4,117 +4,66 @@ const countChar = require("./count");
44
// When the countChar function is called with these inputs,
55
// Then it should:
66

7-
// Scenario: Multiple Occurrences
8-
// Given the input string str,
9-
// And a character char that may occur multiple times with overlaps within str (e.g., 'a' in 'aaaaa'),
10-
// When the function is called with these inputs,
11-
// Then it should correctly count overlapping occurrences of char (e.g., 'a' appears five times in 'aaaaa').
12-
test("should count multiple occurrences of a character", () => {
13-
const str = "aaaaa";
14-
const char = "a";
15-
const count = countChar(str, char);
16-
expect(count).toEqual(5);
7+
const countChar = require("./count");
8+
9+
// Grouped tests for standard character counting
10+
test.each([
11+
["a", "aaaaa", 5],
12+
["l", "hello", 2],
13+
["a", "javascript", 2],
14+
["z", "hello", 0],
15+
["h", "hello", 1],
16+
["o", "hipopotamos' make wonderful pets", 3],
17+
["p", "hipopotamos", 1],
18+
["t", "hipopotamos' are a friendly animal", 1],
19+
["x", "hipopotamos", 0],
20+
["i", "Pneumonoultramicroscopicsilicovolcanoconiosis", 6],
21+
])("should count occurrences of character '%s' in string '%s'", (char, str, expected) => {
22+
expect(countChar(str, char)).toEqual(expected);
1723
});
1824

19-
// Scenario: No Occurrences
20-
// Given the input string str,
21-
// And a character char that does not exist within the case-sensitive str,
22-
// When the function is called with these inputs,
23-
// Then it should return 0, indicating that no occurrences of the char were found in the case-sensitive str.
24-
test("should return 0 if the character does not exist in the string", () => {
25-
expect(countChar("hello", "z")).toEqual(0);
25+
// Edge case: empty string
26+
test("should return error if the string is empty", () => {
27+
expect(countChar("", "a")).toEqual("Error: The string cannot be empty.");
2628
});
2729

28-
// Scenario: Single Occurrence
29-
// Given the input string str,
30-
// And a character char that occurs exactly once in str,
31-
// When the function is called with these inputs,
32-
// Then it should return 1, indicating one occurrence of char in str.
33-
test("should count a single occurrence of a character", () => {
34-
expect(countChar("hello", "h")).toEqual(1);
30+
// Edge case: null or undefined string
31+
test("should return error if the string is null or undefined", () => {
32+
expect(countChar(null, "a")).toEqual("Error: The input string cannot be null or undefined.");
33+
expect(countChar(undefined, "a")).toEqual("Error: The input string cannot be null or undefined.");
3534
});
3635

37-
// Scenario: Empty String
38-
// Given an empty input string str,
39-
// And any character char,
40-
// When the function is called with these inputs,
41-
// Then it should return 0, as there are no characters to count.
42-
test("should return error if the string is empty", () => {
43-
expect(countChar("", "a")).toEqual("Error: The string cannot be empty.");
36+
// Edge case: invalid character input
37+
test("should return error if the character to count is not a single character", () => {
38+
expect(countChar("hello", "")).toEqual("Error: The character to count must be a single character.");
39+
expect(countChar("hello", "ll")).toEqual("Error: The character to count must be a single character.");
40+
expect(countChar("hello", null)).toEqual("Error: The character to count must be a single character.");
41+
expect(countChar("hello", undefined)).toEqual("Error: The character to count must be a single character.");
4442
});
4543

46-
// Scenario: Case Sensitivity
47-
// Given the input string str containing both uppercase and lowercase versions of a letter,
48-
// And a character char that matches only one case (either uppercase or lowercase),
49-
// When the function is called with these inputs,
50-
// Then it should count only the occurrences matching the exact case of char.
44+
// Case sensitivity
5145
test("should count only characters that match the exact case", () => {
5246
const str = "AaAaA";
53-
const lowercaseResult = countChar(str, "a"); // lowercase only
54-
const uppercaseResult = countChar(str, "A"); // uppercase only
55-
expect(lowercaseResult).toEqual(2); // Only lowercase 'a's
56-
expect(uppercaseResult).toEqual(3); // Only uppercase 'A's
47+
expect(countChar(str, "a")).toEqual(2); // lowercase only
48+
expect(countChar(str, "A")).toEqual(3); // uppercase only
5749
});
5850

59-
// Scenario: Special Characters
60-
// Given the input string str containing special or non-alphabetic characters,
61-
// And a character char that matches one of those special characters,
62-
// When the function is called with these inputs,
63-
// Then it should return the number of times that special character occurs in str.
51+
// Special characters
6452
test("should count special characters", () => {
6553
expect(countChar("!?!?", "?")).toEqual(2);
6654
});
6755

68-
// Scenario: Spaces
69-
// Given the input string str that contains space characters,
70-
// And a character char that is a space,
71-
// When the function is called with these inputs,
72-
// Then it should return the number of space characters in str.
56+
// Spaces
7357
test("should count spaces correctly", () => {
7458
expect(countChar("a b c", " ")).toEqual(2);
7559
});
7660

77-
// Scenario: Null or Undefined Input (Optional Defensive Case)
78-
// Given a null or undefined input string str,
79-
// And a character char,
80-
// When the function is called with these inputs,
81-
// Then it should safely return 0 instead of throwing an error.
82-
test("should return error if the string is null or undefined", () => {
83-
expect(countChar(null, "a")).toEqual(
84-
"Error: The input string cannot be null or undefined."
85-
);
86-
expect(countChar(undefined, "a")).toEqual(
87-
"Error: The input string cannot be null or undefined."
88-
);
89-
});
90-
91-
// Scenario: Character Not a Single Character (Optional Defensive Case)
92-
// Given the input string str,
93-
// And a character char that is either an empty string or has more than one character,
94-
// When the function is called with these inputs,
95-
// Then it should return an error indicating that char must be a single character.
96-
test("should return error if the character to count is not a single character", () => {
97-
expect(countChar("hello", "")).toEqual(
98-
"Error: The character to count must be a single character."
99-
);
100-
expect(countChar("hello", "ll")).toEqual(
101-
"Error: The character to count must be a single character."
102-
);
103-
});
104-
105-
// Scenario: Unicode Characters
106-
// Given the input string str containing Unicode characters (e.g., emojis),
107-
// And a character char that is a Unicode character,
108-
// When the function is called with these inputs,
109-
// Then it should return the number of times that Unicode character occurs in str.
61+
// Unicode characters
11062
test("should count unicode characters correctly", () => {
11163
expect(countChar("😀😃😀😄", "😀")).toEqual(2);
11264
});
113-
// Scenario: Long Strings
114-
// Given a very long input string str,
115-
// And a character char that occurs multiple times in str,
116-
// When the function is called with these inputs,
117-
// Then it should efficiently return the correct count of char in str.
65+
66+
// Long strings
11867
test("should handle long strings efficiently", () => {
11968
const longString = "a".repeat(10000) + "b".repeat(5000) + "a".repeat(3000);
12069
expect(countChar(longString, "a")).toEqual(13000);

0 commit comments

Comments
 (0)