Skip to content

Commit c9d6085

Browse files
Complete the first part of the second branch
1 parent a00047b commit c9d6085

File tree

2 files changed

+33
-2
lines changed

2 files changed

+33
-2
lines changed

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

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,33 @@
1-
function countChar(stringOfCharacters, findCharacter) {
2-
return 5
1+
function countChar(fullString,findCharacters) {
2+
let count = 0;
3+
// both of these if statement is for input validation if the input not a string.
4+
if( typeof findCharacters !== "string" || typeof fullString !== "string"){
5+
throw new Error("Invalid input");
6+
};
7+
8+
if (findCharacters.length < 1) {
9+
throw new Error("findCharacters must be a single character");
10+
}
11+
12+
for (let i = 0; i < fullString.length; i++){
13+
if(fullString[i] === findCharacters){
14+
count++;
15+
}
16+
}
17+
return count;
318
}
419

520
module.exports = countChar;
21+
22+
23+
function assertTest(testInput,testCheck){
24+
console.assert(
25+
testInput === testCheck,
26+
`Expect ${testInput} equal to ${testCheck}`
27+
);
28+
};
29+
30+
assertTest(countChar("whale fat hat cat","a"),4)
31+
assertTest(countChar("I need to lean more and know more","e"),5)
32+
assertTest(countChar("the city centre currently have a carnival","c"),4)
33+
assertTest(countChar("the cruise ship in in transit to south America","s"),4)

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,6 @@ 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+
test( "should return count of zero occurrences of a character", () =>{
26+
expect(countChar("go home and study","c")).toEqual(0)
27+
});

0 commit comments

Comments
 (0)