Skip to content

Commit 92afa66

Browse files
author
Payman IB
committed
Implement countChar function and add test for non-existent character
1 parent 8f3d6cf commit 92afa66

File tree

3 files changed

+17
-2
lines changed

3 files changed

+17
-2
lines changed

Project-CLI-Treasure-Hunt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Subproject commit 6668c42dc5412e54026ca0fa8cb5691017ef6350

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

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
function countChar(stringOfCharacters, findCharacter) {
2-
return 5
3-
}
2+
let count = 0;
3+
for (let i = 0; i < stringOfCharacters.length; i++) {
4+
if (stringOfCharacters[i] === findCharacter) {
5+
count++;
6+
}
7+
}
8+
return count;
9+
}
10+
console.log(countChar("hello world!", "l"));
411

512
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 the case-sensitive str,
2323
// When the function is called with these inputs,
2424
// Then it should return 0, indicating that no occurrences of the char were found in the case-sensitive str.
25+
26+
test("should return 0 when character does not exist in the string", () => {
27+
const str = "hello world";
28+
const char = "z";
29+
const count = countChar(str, char);
30+
expect(count).toEqual(0);
31+
});

0 commit comments

Comments
 (0)