-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvowelCount.js
More file actions
50 lines (38 loc) · 1.12 KB
/
vowelCount.js
File metadata and controls
50 lines (38 loc) · 1.12 KB
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
// Write code to return the the number of vowels in `str`
var vowelCount = function(str) {
var result = 0;
var vowels = [ "a", "e", "i", "o", "u" ];
for (var i = 0; i < str.length; i++) {
var letter = str[ i ].toLowerCase();
if (vowels.indexOf(letter) !== -1) {
result += 1;
}
}
return result;
};
// Alternatively, this problem could have been solved without the use of `indexOf`, but by using the logical OR (||) operator to check for each vowel
// var vowelCount = function (str) {
// var result = 0;
// for (var i = 0; i < str.length; i++) {
// var letter = str[i].toLowerCase();
// if (letter === "a" || letter === "e" || letter === "i" || letter === "o" || letter === "u") {
// result += 1;
// }
// }
// return result;
// };
// OTHER ALTERNATIVES
// vowelCount
// function getCount(str) {
// return (str.match(/[aeiou]/ig) || []).length;
// }
// function getCount(str) {
// let vowelsCount = 0
// const vowels = ['a', 'e', 'i', 'o', 'u']
// for (let char of str) {
// if (vowels.includes(char)) {
// vowelsCount++
// }
// }
// return vowelsCount
// }