Skip to content

Commit a09a992

Browse files
refactor: implement character counting logic in countChar function
1 parent dbf085f commit a09a992

File tree

1 file changed

+15
-2
lines changed

1 file changed

+15
-2
lines changed

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

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,18 @@
11
function countChar(stringOfCharacters, findCharacter) {
2-
return 5
2+
// start a count of 0
3+
let count = 0;
4+
5+
// check each of the characters in the string one by one.
6+
for (let i = 0; i < stringOfCharacters.length; i++) {
7+
// checks if the current characters matches the one were looking for in the string.
8+
if (stringOfCharacters[i] === findCharacter)
9+
// if it does, we increment the count by 1.
10+
count = count + 1;
11+
}
12+
13+
return count;
314
}
15+
console.log(countChar("aaaaa", "a")); // 5
16+
console.log(countChar("hello", "l")); // 2
417

5-
module.exports = countChar;
18+
module.exports = countChar;

0 commit comments

Comments
 (0)