Skip to content

Commit ea0b10f

Browse files
author
Connor Leech
committed
add vowels solutions
1 parent 723a5b2 commit ea0b10f

File tree

1 file changed

+51
-0
lines changed

1 file changed

+51
-0
lines changed
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
// --- Directions
2+
// Write a function that returns the number of vowels
3+
// used in a string. Vowels are the characters 'a', 'e'
4+
// 'i', 'o', and 'u'.
5+
// --- Examples
6+
// vowels('Hi There!') --> 3
7+
// vowels('Why do you ask?') --> 4
8+
// vowels('Why?') --> 0
9+
10+
function vowels(str) {
11+
let count = 0;
12+
13+
str.split('').map((letter)=>{
14+
letter = letter.toLowerCase() || null;
15+
if(letter == 'a' || letter == 'e' || letter == 'i' || letter == 'o' || letter == 'u'){
16+
count++;
17+
}
18+
});
19+
20+
return count;
21+
}
22+
23+
24+
25+
26+
// Could uss a for ... of loop
27+
function vowels(str) {
28+
let count = 0;
29+
const checker = ['a', 'e', 'i', 'o', 'u'];
30+
31+
for (let char of str.toLowerCase()) {
32+
if (checker.includes(char)) {
33+
count++;
34+
}
35+
}
36+
37+
return count;
38+
}
39+
40+
41+
42+
43+
// or a regular expression:
44+
function vowels(str) {
45+
const matches = str.match(/[aeiou]/gi);
46+
return matches ? matches.length : 0;
47+
}
48+
49+
50+
51+
console.log(vowels('Why do you ask?'));

0 commit comments

Comments
 (0)