11// implement a function countChar that counts the number of times a character occurs in a string
22const countChar = require ( "./count" ) ;
3-
4- function countChar ( stringOfCharacters , findCharacter ) {
5- let count = 0 ;
6- for ( let char of stringOfCharacters ) {
7- if ( char === findCharacter ) count ++ ;
8- }
9- return count ;
10- }
11-
12- module . exports = countChar ;
13-
143// Given a string str and a single character char to search for,
154// When the countChar function is called with these inputs,
165// Then it should:
@@ -27,36 +16,6 @@ test("should count multiple occurrences of a character", () => {
2716 const count = countChar ( str , char ) ;
2817 expect ( count ) . toEqual ( 5 ) ;
2918} ) ;
30- test ( "should return 0 when the character is not found" , ( ) => {
31- const str = "hello" ;
32- const char = "z" ;
33- const count = countChar ( str , char ) ;
34- expect ( count ) . toEqual ( 0 ) ;
35- } ) ;
36-
37- // Scenario: Case Sensitivity
38- // It should be case-sensitive, meaning 'A' != 'a'.
39- test ( "should be case-sensitive when counting characters" , ( ) => {
40- const str = "Banana" ;
41- expect ( countChar ( str , "a" ) ) . toEqual ( 3 ) ;
42- expect ( countChar ( str , "A" ) ) . toEqual ( 0 ) ;
43- } ) ;
44-
45- // Scenario: Empty String
46- // It should return 0 when the input string is empty.
47- test ( "should return 0 when the input string is empty" , ( ) => {
48- const str = "" ;
49- const char = "a" ;
50- const count = countChar ( str , char ) ;
51- expect ( count ) . toEqual ( 0 ) ;
52- } ) ;
53-
54- // Scenario: Special Characters
55- // It should correctly count spaces and punctuation.
56- test ( "should count special characters like spaces or punctuation" , ( ) => {
57- expect ( countChar ( "a b a b" , " " ) ) . toEqual ( 2 ) ;
58- expect ( countChar ( "wow!!!" , "!" ) ) . toEqual ( 3 ) ;
59- } ) ;
6019
6120// Scenario: No Occurrences
6221// Given the input string str,
0 commit comments