File tree Expand file tree Collapse file tree 1 file changed +34
-0
lines changed
Expand file tree Collapse file tree 1 file changed +34
-0
lines changed Original file line number Diff line number Diff line change 1+ /**
2+ *
13function countChar(stringOfCharacters, findCharacter) {
24 return 5
35}
46
57module.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+
You can’t perform that action at this time.
0 commit comments