-
Notifications
You must be signed in to change notification settings - Fork 149
Expand file tree
/
Copy pathpartA_sol4.js
More file actions
36 lines (30 loc) · 875 Bytes
/
partA_sol4.js
File metadata and controls
36 lines (30 loc) · 875 Bytes
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
// Step 1: Set count = 0
// Step 2: Store the vowels in an object (vowels) with initial values as zero
// Step 3: Iterate through each character of string and check whether it exists in the vowels object
// Step 4: If it exists, increment its value, and the value of count variable
// Step 5: Print the result
function numVowels (str) {
let count = 0;
let vowels = {
'a': 0,
'e': 0,
'i': 0,
'o': 0,
'u': 0
};
for (let char of str) {
// if (vowels[char]) {
// vowels [char]++;
// count++;
// }
for (vowel in vowels) {
if (char.toLowerCase() === vowel) {
count++;
vowels[vowel]++
}
}
}
console.log (`The number of vowels in ${str} is = ${count}`);
}
numVowels ('hello');
numVowels ('Greetings');