1+ /**
2+ *
13// implement a function countChar that counts the number of times a character occurs in a string
24const countChar = require("./count");
35// Given a string `str` and a single character `char` to search for,
@@ -22,3 +24,88 @@ test("should count multiple occurrences of a character", () => {
2224// And a character `char` that does not exist within `str`.
2325// When the function is called with these inputs,
2426// Then it should return 0, indicating that no occurrences of `char` were found.
27+ *
28+ */
29+
30+ // count.test.js
31+
32+ // implement a function countChar that counts the number of times a character occurs in a string
33+ const countChar = require ( "./count" ) ;
34+
35+ // Scenario: Multiple Occurrences
36+ test ( "should count multiple occurrences of a character" , ( ) => {
37+ const str = "aaaaa" ;
38+ const char = "a" ;
39+ const count = countChar ( str , char ) ;
40+ expect ( count ) . toEqual ( 5 ) ;
41+ } ) ;
42+
43+ // Scenario: No Occurrences
44+ test ( "should return 0 when character does not exist in string" , ( ) => {
45+ const str = "hello world" ;
46+ const char = "z" ;
47+ const count = countChar ( str , char ) ;
48+ expect ( count ) . toEqual ( 0 ) ;
49+ } ) ;
50+
51+ // Additional test cases to ensure robustness:
52+
53+ // Scenario: Single Occurrence
54+ test ( "should count a single occurrence of a character" , ( ) => {
55+ const str = "hello" ;
56+ const char = "e" ;
57+ const count = countChar ( str , char ) ;
58+ expect ( count ) . toEqual ( 1 ) ;
59+ } ) ;
60+
61+ // Scenario: Character appears at the beginning and end
62+ test ( "should count character at beginning and end of string" , ( ) => {
63+ const str = "abracadabra" ;
64+ const char = "a" ;
65+ const count = countChar ( str , char ) ;
66+ expect ( count ) . toEqual ( 5 ) ;
67+ } ) ;
68+
69+ // Scenario: Case sensitivity
70+ test ( "should be case sensitive when counting characters" , ( ) => {
71+ const str = "Hello World" ;
72+ const char = "h" ;
73+ const count = countChar ( str , char ) ;
74+ expect ( count ) . toEqual ( 0 ) ; // 'H' is uppercase, 'h' is lowercase
75+ } ) ;
76+
77+ // Scenario: Character is a space
78+ test ( "should count spaces when searching for space character" , ( ) => {
79+ const str = "hello world how are you" ;
80+ const char = " " ;
81+ const count = countChar ( str , char ) ;
82+ expect ( count ) . toEqual ( 4 ) ;
83+ } ) ;
84+
85+ // Scenario: Character is a number
86+ test ( "should count numeric characters" , ( ) => {
87+ const str = "abc123abc123abc" ;
88+ const char = "1" ;
89+ const count = countChar ( str , char ) ;
90+ expect ( count ) . toEqual ( 2 ) ;
91+ } ) ;
92+
93+ // Scenario: Empty string
94+ test ( "should return 0 when string is empty" , ( ) => {
95+ const str = "" ;
96+ const char = "a" ;
97+ const count = countChar ( str , char ) ;
98+ expect ( count ) . toEqual ( 0 ) ;
99+ } ) ;
100+
101+ // Scenario: Character is longer than one character (edge case)
102+ test ( "should handle when char parameter is longer than one character" , ( ) => {
103+ const str = "hello" ;
104+ const char = "ll" ;
105+ const count = countChar ( str , char ) ;
106+ // Depending on requirements, this might count occurrences of the substring
107+ // For this implementation, we'll specify it should count the first character only
108+ expect ( count ) . toEqual ( 2 ) ; // Counting 'l' occurrences
109+ } ) ;
110+
111+
0 commit comments