-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDay3.js
More file actions
38 lines (28 loc) · 1.13 KB
/
Day3.js
File metadata and controls
38 lines (28 loc) · 1.13 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
31
32
33
34
35
36
37
38
// Interview Question: Count Occurrences of Character
// Task:
// Write a function called countChar that takes two parameters: a string and a character to count.
// The function should return the number of times the specified character appears in the string.
// Example:
function countChar(str, char) {
// Convert the input string and specified character to lowercase
str = str.toLowerCase();
char = char.toLowerCase();
let count = 0;
// Iterate through each character in the string
for (let i = 0; i < str.length; i++) {
// Check if the current character matches the specified character
if (str.charAt(i) === char) {
// Increment count if the characters match
count++;
}
}
// Return the total count of occurrences
return count;
}
// Test the function with an example
console.log(countChar("MissIssippi", "I")); // Output: 4
// Test the function with an example
// Constraints:
// - The function should be case-sensitive.
// - The function should handle both lowercase and uppercase characters.
// - The character parameter can be any printable ASCII character.