Skip to content

Commit 447094d

Browse files
committed
Completed count.js
1 parent 3372770 commit 447094d

2 files changed

Lines changed: 36 additions & 1 deletion

File tree

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

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,18 @@
11
function countChar(stringOfCharacters, findCharacter) {
2-
return 5
2+
// trying to find the amount of times the findCharacter is found in the stringOfCharacters
3+
const lengthOfString = stringOfCharacters.length;
4+
console.log("lengthOfString", lengthOfString);
5+
6+
// replace all instances of findCharacter in string of characters with an empty string, and then get the length. (e.g. in house replace the e with an empty string " ").
7+
const otherLettersLength = stringOfCharacters.replaceAll(
8+
findCharacter,
9+
""
10+
).length;
11+
12+
// declaring the variable called result and subtract otherLettersLength from the lengthOfString to find the number of occurences of findCharacter.
13+
const result = lengthOfString - otherLettersLength;
14+
console.log("result", result);
15+
return result;
316
}
417

518
module.exports = countChar;

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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,30 @@ test("should count multiple occurrences of a character", () => {
1717
expect(count).toEqual(5);
1818
});
1919

20+
test("should count multiple occurrences of a character", () => {
21+
const str = "house";
22+
const char = "e";
23+
const count = countChar(str, char);
24+
expect(count).toEqual(1);
25+
});
26+
27+
test("should count multiple occurrences of a character", () => {
28+
const str = "playful";
29+
const char = "l";
30+
const count = countChar(str, char);
31+
expect(count).toEqual(2);
32+
});
33+
2034
// Scenario: No Occurrences
2135
// Given the input string `str`,
2236
// And a character `char` that does not exist within `str`.
2337
// When the function is called with these inputs,
2438
// Then it should return 0, indicating that no occurrences of `char` were found.
39+
40+
// test(``);
41+
test("should return when there are no occurrences of a character", () => {
42+
const str = "spaceship";
43+
const char = "x";
44+
const count = countChar(str, char);
45+
expect(count).toEqual(0);
46+
});

0 commit comments

Comments
 (0)