-
Notifications
You must be signed in to change notification settings - Fork 0
/
NumberOfVowelsInAString.js
54 lines (43 loc) · 1.2 KB
/
NumberOfVowelsInAString.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
50
51
52
53
54
/**
* Given a string, count the number of vowels present.
*/
// Split the string into an array, and use a for loop to iterate through
// the letters. Use a switch statement for beginner purposes.
const getCount = str => {
let vowelsCount = 0
const arr = str.split("")
for(let i = 0; i < arr.length; i++) {
switch(arr[i]) {
case 'a':
vowelsCount++
break;
case 'e':
vowelsCount++
break;
case 'i':
vowelsCount++
break;
case 'o':
vowelsCount++
break;
case 'u':
vowelsCount++
break;
}
}
return vowelsCount
}
console.log(getCount("My name is Stefan Bayne"))
// prints 7 vowels
/**
* We could also solve this using match() and includes() with a regex.
*/
// program to count the number of vowels in a string
const countVowel = stringInput => {
// find the count of vowels
const count = stringInput.match(/[aeiou]/gi).length;
// return number of vowels
return count;
}
console.log(countVowel("My name is Stefan Lamario Bayne!"))
// prints 11 vowels