Skip to content

Commit 6364ddd

Browse files
committed
Count.js function
1 parent 21048fe commit 6364ddd

File tree

1 file changed

+26
-1
lines changed

1 file changed

+26
-1
lines changed

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

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,30 @@
11
function countChar(stringOfCharacters, findCharacter) {
2-
return 5
2+
if (
3+
typeof stringOfCharacters != "string" ||
4+
typeof findCharacter != "string"
5+
) {
6+
throw new Error("Please enter a valid string");
7+
}
8+
9+
if (findCharacter.length != 1) {
10+
throw new Error(
11+
"Please enter a single character for findCharacter, e.g. 't"
12+
);
13+
}
14+
15+
if (stringOfCharacters.length === 0) {
16+
throw new Error(
17+
"Please enter a non-empty string, e.g. 'A series of unfortunate events'"
18+
);
19+
}
20+
21+
let charCount = 0;
22+
let lowerCaseChar = findCharacter.toLowerCase();
23+
for (char of stringOfCharacters.toLowerCase()) {
24+
if (lowerCaseChar === char) charCount++;
25+
}
26+
27+
return charCount;
328
}
429

530
module.exports = countChar;

0 commit comments

Comments
 (0)