-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAllStarCC#18.js
More file actions
30 lines (27 loc) · 1.06 KB
/
AllStarCC#18.js
File metadata and controls
30 lines (27 loc) · 1.06 KB
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
/* Instructions:
Create a function that accepts a string and a single character, and returns an integer of the count of occurrences the 2nd argument is found in the first one.
If no occurrences can be found, a count of 0 should be returned.
("Hello", "o") ==> 1
("Hello", "l") ==> 2
("", "z") ==> 0
str_count("Hello", 'o'); // returns 1
str_count("Hello", 'l'); // returns 2
str_count("", 'z'); // returns 0
Notes
The first argument can be an empty string
In languages with no distinct character data type, the second argument will be a string of length 1
*/
//Answer
function strCount(str, letter) {
let array = str.split("");
let count = 0; // Initialize a count variable to keep track of occurrences
for (let char of array) {
// Use 'char' instead of 'a' for clarity
if (letter === char) {
// Compare 'letter' with each character of 'array'
count++; // Increment count when 'letter' matches the current character
}
}
return count; // Return the count of occurrences of 'letter'
}
console.log(strCount("Hello", "o")); // Output: 1