File tree Expand file tree Collapse file tree 1 file changed +54
-0
lines changed Expand file tree Collapse file tree 1 file changed +54
-0
lines changed Original file line number Diff line number Diff line change 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 ( / [ a e i o u ] / 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
You can’t perform that action at this time.
0 commit comments