-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcount.js
More file actions
24 lines (22 loc) · 754 Bytes
/
Copy pathcount.js
File metadata and controls
24 lines (22 loc) · 754 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
function countChar(stringOfCharacters, findCharacter) {
if (arguments.length !== 2) {
throw new Error(
"Function requires exactly two arguments: a string and a character to find."
);
}
if (typeof stringOfCharacters !== 'string'){
throw new Error("First argument must be a string.");
}
if (typeof findCharacter !== "string") {
throw new Error("Second argument must be a string.");
}
if (findCharacter.length !== 1) {
throw new Error("Character to find must be a single character.");
}
if (stringOfCharacters.length === 0) {
return 0;
}
console.log(Array.from(stringOfCharacters));
return Array.from(stringOfCharacters).filter(char => char === findCharacter).length;
}
module.exports = countChar;