-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProblem-2_Count_Vowels_in_a_String.js
More file actions
42 lines (33 loc) · 1.59 KB
/
Problem-2_Count_Vowels_in_a_String.js
File metadata and controls
42 lines (33 loc) · 1.59 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
39
// Problem-2_Count_Vowels_in_a_String with Solution, Explanation, Verification, and Execution:
// To count vowels in a string, we can implement the Solution like below:
// -------------------------------------------------------------------
function countVowels(str) {
let count = 0;
const vowels = 'aeiouAEIOU';
for (let char of str) {
if (vowels.includes(char)) {
count++;
}
}
return count;
}
// Explanation:
// ---------------
// The function starts with a counter set to zero and defines a string containing all vowels (both lowercase and uppercase for case-insensitivity). It loops through each character in the input string using a for-of loop. For each character, it checks if it is included in the vowels string; if so, the counter increments. This method scans the string sequentially, tallying matches without relying on regular expressions or complex filtering, making it simple to follow.
// Verification:
// ----------------
// function countVowels(programming) {
// let count = 0;
// const vowels = 'aeiouAEIOU';
// for (let char of programming) {
// if (vowels.includes(char)) {
// count++;
// }
// }
// return count;
// }
// console.log(countVowels("programming"));
// Execution of the above code block in IDE environment with node.js installed:
// ----------------------------------------------------------------------
// Input (In the directory in IDE environment with node.js installed): 'node Problem-2_Count_Vowels_in_a_String.js'
// Output (In the directory in IDE environment with node.js installed): 3