Skip to content

Commit cf3d5cd

Browse files
fix countChar function to handle empty character cases and improve iteration logic
1 parent a2ae864 commit cf3d5cd

File tree

1 file changed

+6
-2
lines changed

1 file changed

+6
-2
lines changed

Sprint-3/2-practice-tdd/count.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
function countChar(stringOfCharacters, findCharacter) {
22
let char = 0;
3-
for(let i = 0;i < stringOfCharacters.length;i++){
4-
if(stringOfCharacters[i] === findCharacter){
3+
for(let i = 0;i <= stringOfCharacters.length - findCharacter.length;i++){
4+
if ( findCharacter.length === 0 || stringOfCharacters === 0){
5+
return 0;
6+
}
7+
if(stringOfCharacters.substring(i, i + findCharacter.length ) === findCharacter){
58
char++;
69
}
710
}
811
return char
912
}
1013

1114
module.exports = countChar;
15+
console.log(countChar("aaaaa","a"));

0 commit comments

Comments
 (0)