File tree Expand file tree Collapse file tree 2 files changed +39
-1
lines changed
Expand file tree Collapse file tree 2 files changed +39
-1
lines changed Original file line number Diff line number Diff line change 11function countChar ( stringOfCharacters , findCharacter ) {
2- return 5
2+ let counter = 0 ;
3+ let index = stringOfCharacters . indexOf ( findCharacter ) ;
4+ while ( index !== - 1 )
5+ {
6+ counter ++ ;
7+
8+ index = stringOfCharacters . indexOf ( findCharacter , index + 1 ) ;
9+ }
10+ return counter ;
311}
412
513module . exports = countChar ;
14+ function assertFunction ( currentOutput , targetOutput )
15+ {
16+ console . assert ( currentOutput === targetOutput ,
17+ `expect ${ currentOutput } to equal ${ targetOutput } `
18+ ) ;
19+ }
20+ assertFunction ( countChar ( "Ahmaaa hmaaa" , 'a' ) , 6 ) ;
21+ assertFunction ( countChar ( "Ahmaaa hmaaa" , 'm' ) , 2 ) ;
22+ assertFunction ( countChar ( "Ahmaaa hmaaa" , 'b' ) , 0 ) ;
23+ assertFunction ( countChar ( "Ahmaaa hmaaa" , 'A' ) , 1 ) ;
24+ assertFunction ( countChar ( "" , 'A' ) , 0 ) ;
Original file line number Diff line number Diff line change @@ -22,3 +22,22 @@ test("should count multiple occurrences of a character", () => {
2222// And a character char that does not exist within the case-sensitive str,
2323// When the function is called with these inputs,
2424// Then it should return 0, indicating that no occurrences of the char were found in the case-sensitive str.
25+ test ( "should return 0 occurrences of b characters " , ( ) => {
26+ const str = "Ahmad Hmedan" ;
27+ const char = "b" ;
28+ const count = countChar ( str , char ) ;
29+ expect ( count ) . toEqual ( 0 ) ;
30+ } )
31+
32+ test ( "should return 1 occurrence of A characters" , ( ) => {
33+ const str = "Ahmad Hmedan" ;
34+ const char = "A" ;
35+ const count = countChar ( str , char ) ;
36+ expect ( count ) . toEqual ( 1 ) ;
37+ } )
38+ test ( "should return 2 occurrence of m characters" , ( ) => {
39+ const str = "Ahmad Hmedan" ;
40+ const char = "m" ;
41+ const count = countChar ( str , char ) ;
42+ expect ( count ) . toEqual ( 2 ) ;
43+ } )
You can’t perform that action at this time.
0 commit comments