11// implement a function countChar that counts the number of times a character occurs in a string
2- const 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 ;
132
143// Given a string str and a single character char to search for,
154// When the countChar function is called with these inputs,
@@ -20,6 +9,9 @@ module.exports = countChar;
209// And a character char that may occur multiple times with overlaps within str (e.g., 'a' in 'aaaaa'),
2110// When the function is called with these inputs,
2211// Then it should correctly count overlapping occurrences of char (e.g., 'a' appears five times in 'aaaaa').
12+
13+ const countChar = require ( './count' ) ;
14+
2315
2416test ( "should count multiple occurrences of a character" , ( ) => {
2517 const str = "aaaaa" ;
@@ -54,7 +46,7 @@ test("should return 0 when the input string is empty", () => {
5446// Scenario: Special Characters
5547// It should correctly count spaces and punctuation.
5648test ( "should count special characters like spaces or punctuation" , ( ) => {
57- expect ( countChar ( "a b a b" , " " ) ) . toEqual ( 2 ) ;
49+ expect ( countChar ( "a b a b" , " " ) ) . toEqual ( 3 ) ;
5850 expect ( countChar ( "wow!!!" , "!" ) ) . toEqual ( 3 ) ;
5951} ) ;
6052
0 commit comments