-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcount.js
More file actions
25 lines (22 loc) · 676 Bytes
/
Copy pathcount.js
File metadata and controls
25 lines (22 loc) · 676 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
function countChar(stringOfCharacters, findCharacter) {
let arrayOfCharacters = [];
if (typeof stringOfCharacters === "string") {
arrayOfCharacters = stringOfCharacters.split("");
} else {
throw new Error("Input str should be a string");
}
if (typeof findCharacter !== "string") {
throw new Error("Input char should be a string");
}
if (findCharacter.length !== 1) {
throw new Error("Input char should be a single character");
}
let count = 0;
for (let index = 0; index < arrayOfCharacters.length; index++) {
if (arrayOfCharacters[index] === findCharacter) {
count += 1;
}
}
return count;
}
module.exports = countChar;