-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcountVowels.js
50 lines (38 loc) · 1.1 KB
/
countVowels.js
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
40
41
42
43
44
45
46
47
48
49
/**
* -------------------------------------------------------
* Programming Question : Count Vowels
* -------------------------------------------------------
**/
// Q. Write a function that takes a string as input and returns the count of vowels in that string. Consider 'a','e','i','o','u' as vowels(both lowercase and uppercase).
//constraint
//?
//?
//?
//?
function countVowels(str) {
str = str.toLowerCase().split("");
let count = 0;
//Method 1
// let result = str.filter(s => {
// if(s === 'a' || s === 'e' || s === 'i' || s === 'o' || s === 'u'){
// ++count;
// }
// })
//Method 2
// for(let i=0;i<str.length;i++){
// if(str[i] === 'a' || str[i] === 'e' || str[i] === 'i' || str[i] === 'o' || str[i] === 'u' ){
// ++count;
// }
// }
//Method 3
let vowels = ['a', 'e', 'o', 'i', 'u'];
for (let char of str) {
if (vowels.includes(char)) {
count++;
}
}
return count;
}
console.log(countVowels("Helloo world"));
console.log(countVowels("ThE quIck brOwn fOx"));
console.log(countVowels("Brrrp "));