Skip to content

Commit 4a496e6

Browse files
committed
Count all the vowels in a string
1 parent ca8e0f0 commit 4a496e6

File tree

1 file changed

+54
-0
lines changed

1 file changed

+54
-0
lines changed

NumberOfVowelsInAString.js

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/**
2+
* Given a string, count the number of vowels present.
3+
*/
4+
5+
// Split the string into an array, and use a for loop to iterate through
6+
// the letters. Use a switch statement for beginner purposes.
7+
8+
const getCount = str => {
9+
let vowelsCount = 0
10+
const arr = str.split("")
11+
12+
for(let i = 0; i < arr.length; i++) {
13+
switch(arr[i]) {
14+
case 'a':
15+
vowelsCount++
16+
break;
17+
case 'e':
18+
vowelsCount++
19+
break;
20+
case 'i':
21+
vowelsCount++
22+
break;
23+
case 'o':
24+
vowelsCount++
25+
break;
26+
case 'u':
27+
vowelsCount++
28+
break;
29+
}
30+
}
31+
32+
return vowelsCount
33+
}
34+
35+
console.log(getCount("My name is Stefan Bayne"))
36+
// prints 7 vowels
37+
38+
/**
39+
* We could also solve this using match() and includes() with a regex.
40+
*/
41+
42+
// program to count the number of vowels in a string
43+
44+
const countVowel = stringInput => {
45+
46+
// find the count of vowels
47+
const count = stringInput.match(/[aeiou]/gi).length;
48+
49+
// return number of vowels
50+
return count;
51+
}
52+
53+
console.log(countVowel("My name is Stefan Lamario Bayne!"))
54+
// prints 11 vowels

0 commit comments

Comments
 (0)