File tree Expand file tree Collapse file tree 1 file changed +15
-13
lines changed
Expand file tree Collapse file tree 1 file changed +15
-13
lines changed Original file line number Diff line number Diff line change 11function countChar ( stringOfCharacters , findCharacter ) {
2- let characterOccurrence = "" ;
2+ // Check both inputs are strings
33 if (
4- typeof stringOfCharacters === "string" &&
5- typeof findCharacter = == "string"
4+ typeof stringOfCharacters !== "string" ||
5+ typeof findCharacter ! == "string"
66 ) {
7- stringOfCharacters = stringOfCharacters . toLowerCase ( ) ;
8- findCharacter = findCharacter . toLowerCase ( ) ;
9- if ( findCharacter . length === 1 ) {
10- characterOccurrence = stringOfCharacters . split ( findCharacter ) . length - 1 ;
11- } else {
12- characterOccurrence = "invalid input: Input just one character" ;
13- }
14- } else {
15- characterOccurrence = "Invalid input: input should be a string" ;
7+ return "Invalid input: input should be a string" ;
168 }
179
18- return characterOccurrence ;
10+ // Convert both to lowercase for case-insensitive matching
11+ const str = stringOfCharacters . toLowerCase ( ) ;
12+ const char = findCharacter . toLowerCase ( ) ;
13+
14+ // Check that only one character is passed
15+ if ( char . length !== 1 ) {
16+ return "invalid input: Input just one character" ;
17+ }
18+
19+ // Return count (0 for empty strings works naturally)
20+ return str . split ( char ) . length - 1 ;
1921}
2022
2123module . exports = countChar ;
You can’t perform that action at this time.
0 commit comments