Skip to content

Commit a21fb5e

Browse files
committed
Implement a countChar function and test with jest
1 parent 8f3d6cf commit a21fb5e

File tree

2 files changed

+39
-1
lines changed

2 files changed

+39
-1
lines changed

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

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,24 @@
11
function countChar(stringOfCharacters, findCharacter) {
2-
return 5
2+
let counter=0;
3+
let index=stringOfCharacters.indexOf(findCharacter);
4+
while(index!== -1)
5+
{
6+
counter++;
7+
8+
index = stringOfCharacters.indexOf(findCharacter,index+1);
9+
}
10+
return counter;
311
}
412

513
module.exports = countChar;
14+
function assertFunction(currentOutput,targetOutput)
15+
{
16+
console.assert(currentOutput===targetOutput,
17+
`expect ${currentOutput} to equal ${targetOutput}`
18+
);
19+
}
20+
assertFunction(countChar("Ahmaaa hmaaa",'a'),6);
21+
assertFunction(countChar("Ahmaaa hmaaa",'m'),2 );
22+
assertFunction(countChar("Ahmaaa hmaaa",'b'),0);
23+
assertFunction(countChar("Ahmaaa hmaaa",'A'),1);
24+
assertFunction(countChar("",'A'),0);

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,22 @@ 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+
test("should return 0 occurrences of b characters ",()=>{
26+
const str="Ahmad Hmedan";
27+
const char= "b";
28+
const count=countChar(str,char);
29+
expect(count).toEqual(0);
30+
})
31+
32+
test("should return 1 occurrence of A characters",()=>{
33+
const str = "Ahmad Hmedan";
34+
const char = "A";
35+
const count = countChar(str, char);
36+
expect(count).toEqual(1);
37+
})
38+
test("should return 2 occurrence of m characters",()=>{
39+
const str = "Ahmad Hmedan";
40+
const char = "m";
41+
const count = countChar(str, char);
42+
expect(count).toEqual(2);
43+
})

0 commit comments

Comments
 (0)