-
-
Notifications
You must be signed in to change notification settings - Fork 336
Expand file tree
/
Copy pathcount.js
More file actions
33 lines (27 loc) · 944 Bytes
/
count.js
File metadata and controls
33 lines (27 loc) · 944 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
function countChar(fullString,findCharacters) {
let count = 0;
// both of these if statement is for input validation if the input not a string.
if( typeof findCharacters !== "string" || typeof fullString !== "string"){
throw new Error("Invalid input");
};
if (findCharacters.length < 1) {
throw new Error("findCharacters must be a single character");
}
for (let i = 0; i < fullString.length; i++){
if(fullString[i] === findCharacters){
count++;
}
}
return count;
}
module.exports = countChar;
function assertTest(testInput,testCheck){
console.assert(
testInput === testCheck,
`Expect ${testInput} equal to ${testCheck}`
);
};
assertTest(countChar("whale fat hat cat","a"),4)
assertTest(countChar("I need to lean more and know more","e"),5)
assertTest(countChar("the city centre currently have a carnival","c"),4)
assertTest(countChar("the cruise ship in in transit to south America","s"),4)