File tree Expand file tree Collapse file tree 2 files changed +33
-2
lines changed
Expand file tree Collapse file tree 2 files changed +33
-2
lines changed Original file line number Diff line number Diff line change 1- function countChar ( stringOfCharacters , findCharacter ) {
2- return 5
1+ function countChar ( fullString , findCharacters ) {
2+ let count = 0 ;
3+ // both of these if statement is for input validation if the input not a string.
4+ if ( typeof findCharacters !== "string" || typeof fullString !== "string" ) {
5+ throw new Error ( "Invalid input" ) ;
6+ } ;
7+
8+ if ( findCharacters . length < 1 ) {
9+ throw new Error ( "findCharacters must be a single character" ) ;
10+ }
11+
12+ for ( let i = 0 ; i < fullString . length ; i ++ ) {
13+ if ( fullString [ i ] === findCharacters ) {
14+ count ++ ;
15+ }
16+ }
17+ return count ;
318}
419
520module . exports = countChar ;
21+
22+
23+ function assertTest ( testInput , testCheck ) {
24+ console . assert (
25+ testInput === testCheck ,
26+ `Expect ${ testInput } equal to ${ testCheck } `
27+ ) ;
28+ } ;
29+
30+ assertTest ( countChar ( "whale fat hat cat" , "a" ) , 4 )
31+ assertTest ( countChar ( "I need to lean more and know more" , "e" ) , 5 )
32+ assertTest ( countChar ( "the city centre currently have a carnival" , "c" ) , 4 )
33+ assertTest ( countChar ( "the cruise ship in in transit to south America" , "s" ) , 4 )
Original file line number Diff line number Diff line change @@ -22,3 +22,6 @@ test("should count multiple occurrences of a character", () => {
2222// And a character `char` that does not exist within `str`.
2323// When the function is called with these inputs,
2424// Then it should return 0, indicating that no occurrences of `char` were found.
25+ test ( "should return count of zero occurrences of a character" , ( ) => {
26+ expect ( countChar ( "go home and study" , "c" ) ) . toEqual ( 0 )
27+ } ) ;
You can’t perform that action at this time.
0 commit comments