Skip to content

Commit 5b1b17c

Browse files
committed
count.jscommitted
1 parent 1d00f89 commit 5b1b17c

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

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

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,39 @@
1+
/**
2+
*
13
function countChar(stringOfCharacters, findCharacter) {
24
return 5
35
}
46
57
module.exports = countChar;
8+
*
9+
*/
10+
11+
function countChar(stringOfCharacters, findCharacter) {
12+
// Input validation - return 0 for invalid inputs
13+
if (typeof stringOfCharacters !== 'string' || typeof findCharacter !== 'string') {
14+
return 0;
15+
}
16+
17+
// Return 0 if either parameter is empty
18+
if (stringOfCharacters.length === 0 || findCharacter.length === 0) {
19+
return 0;
20+
}
21+
22+
// Use only the first character of findCharacter (as per test expectations)
23+
const searchChar = findCharacter[0];
24+
25+
let count = 0;
26+
27+
// Count occurrences of the character
28+
for (let i = 0; i < stringOfCharacters.length; i++) {
29+
// Case-sensitive comparison
30+
if (stringOfCharacters[i] === searchChar) {
31+
count++;
32+
}
33+
}
34+
35+
return count;
36+
}
37+
38+
module.exports = countChar;
39+

0 commit comments

Comments
 (0)