-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcount.js
More file actions
25 lines (20 loc) · 620 Bytes
/
Copy pathcount.js
File metadata and controls
25 lines (20 loc) · 620 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) {
if (typeof stringOfCharacters != "string") {
throw new Error("Input should be a string");
}
if (typeof findCharacter !== "string" || findCharacter.length != 1) {
throw new Error("Input must be a single character");
}
let count = 0;
for (let char of stringOfCharacters) {
if (char === findCharacter) {
count++;
}
}
return count;
}
module.exports = countChar;
console.log(countChar("amazon", "a"));
// Added lines to check cases and return count of characters.
// Indentation improved.
// count.js formatted with prettier.