11// implement a function countChar that counts the number of times a character occurs in a string
22const countChar = require ( "./count" ) ;
3+
4+
35// Given a string str and a single character char to search for,
46// When the countChar function is called with these inputs,
5- // Then it should:
7+ // Scenario: Single Occurrence
68
9+ test ( "counts a single occurrence of a character" , ( ) => {
10+ expect ( countChar ( "hello" , "h" ) ) . toEqual ( 1 ) ;
11+ expect ( countChar ( "world" , "d" ) ) . toEqual ( 1 ) ;
12+ } ) ;
713// Scenario: Multiple Occurrences
814// Given the input string str,
915// And a character char that may occur multiple times with overlaps within str (e.g., 'a' in 'aaaaa'),
1016// When the function is called with these inputs,
1117// Then it should correctly count overlapping occurrences of char (e.g., 'a' appears five times in 'aaaaa').
18+ test ( "counts multiple occurrences of a character" , ( ) => {
19+ expect ( countChar ( "banana" , "a" ) ) . toEqual ( 3 ) ;
20+ expect ( countChar ( "mississippi" , "s" ) ) . toEqual ( 4 ) ;
21+ } ) ;
1222
1323test ( "should count multiple occurrences of a character" , ( ) => {
1424 const str = "aaaaa" ;
15- const char = "a" ;
16- const count = countChar ( str , char ) ;
25+ const Char = "a" ;
26+ const count = countChar ( str , Char ) ;
1727 expect ( count ) . toEqual ( 5 ) ;
1828} ) ;
1929
@@ -22,3 +32,11 @@ test("should count multiple occurrences of a character", () => {
2232// And a character char that does not exist within the case-sensitive str,
2333// When the function is called with these inputs,
2434// Then it should return 0, indicating that no occurrences of the char were found in the case-sensitive str.
35+ // Scenario: No Occurrences
36+ test ( "should return 0 if the character is not in the string" , ( ) => {
37+ const str = "hello" ;
38+ const char = "x" ; // character not present in str
39+ const count = countChar ( str , char ) ;
40+ expect ( count ) . toEqual ( 0 ) ;
41+ } ) ;
42+
0 commit comments