Skip to content

Commit 1c66099

Browse files
committed
Update countChar implementation to correctly count character occurrences
1 parent bd7847f commit 1c66099

File tree

1 file changed

+13
-1
lines changed

1 file changed

+13
-1
lines changed

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

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,17 @@
11
function countChar(stringOfCharacters, findCharacter) {
2-
const characterOccurrence = stringOfCharacters.split(findCharacter).length - 1;
2+
let characterOccurrence = "";
3+
if (typeof stringOfCharacters === "string" && typeof findCharacter === "string") {
4+
stringOfCharacters = stringOfCharacters.toLowerCase();
5+
findCharacter = findCharacter.toLowerCase();
6+
if (findCharacter.length === 1) {
7+
characterOccurrence = stringOfCharacters.split(findCharacter).length - 1;
8+
} else {
9+
characterOccurrence = "invalid input: Input just one character";
10+
}
11+
} else {
12+
characterOccurrence = "Invalid input: input should be a string";
13+
}
14+
315
return characterOccurrence;
416
}
517

0 commit comments

Comments
 (0)