Skip to content

Commit 0141efb

Browse files
(count.js) Implement character counting logic in countChar function and (count.test.js) add test for zero occurrences
1 parent 3372770 commit 0141efb

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

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

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,17 @@
11
function countChar(stringOfCharacters, findCharacter) {
2-
return 5
2+
// 1. Assign a variable to keep track of the count
3+
let count = 0;
4+
5+
// 2. Loop through all chars in the string
6+
for (let char = 0; char < stringOfCharacters.length; char++) {
7+
// 3. Check if the current char matches the one we are looking for
8+
if (stringOfCharacters[char] === findCharacter) {
9+
count++; // If it's a match, add 1 to our tally
10+
}
11+
}
12+
13+
// 4. Return the final tally after the loop finishes
14+
return count;
315
}
416

517
module.exports = countChar;

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,10 @@ test("should count multiple occurrences of a character", () => {
2222
// And a character `char` that does not exist within `str`.
2323
// When the function is called with these inputs,
2424
// Then it should return 0, indicating that no occurrences of `char` were found.
25+
26+
test("should return 0 when the character does not occur in the string", () => {
27+
const str = "Will this work?";
28+
const char = "z";
29+
const count = countChar(str, char);
30+
expect(count).toEqual(0);
31+
});

0 commit comments

Comments
 (0)