forked from CodeToExpress/dailycodebase
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpartA_sol2.js
More file actions
23 lines (19 loc) · 761 Bytes
/
partA_sol2.js
File metadata and controls
23 lines (19 loc) · 761 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
// Step 1: Set count = 0
// Step 2: Store the vowels in an array (say, vowels)
// Step 3: Run a loop from i=0 to i=string_length
// Step 4: In each iteration check whether the current character (at position i) of the string is there in the vowels array using indexOf() method
// Step 5: Increment the count if the above condition is satisfied.
// Step 6: Print the result
function numVowels (str) {
let count = 0;
let vowels = ['a', 'e', 'i', 'o', 'u'];
// Iterate over each character in the string
for (let i=0; i<str.length; i++) {
if (vowels.indexOf(str[i].toLowerCase()) > 0) {
count++;
}
}
console.log (`The number of vowels in ${str} is = ${count}`);
}
numVowels ('hello');
numVowels ('Greetings');