-
-
Notifications
You must be signed in to change notification settings - Fork 44
/
Copy pathvowel-count.js
69 lines (59 loc) · 1.32 KB
/
vowel-count.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
function getCount(str) {
let vowelsCount = 0;
// iterate over the string
for (let i = 0; i < str.length; i++) {
const currentLetter = str[i];
// if the current letter is a, e, i, o or u
if (currentLetter == 'a' || currentLetter == 'e' || currentLetter == 'i' || currentLetter == 'o' || currentLetter == 'u') {
// increase the vowelsCount
vowelsCount++;
}
}
return vowelsCount;
}
function getCount(str) {
let vowelsCount = 0;
const vowels = {
a: true,
e: true,
i: true,
o: true,
u: true
};
// iterate over the string
for (let i = 0; i < str.length; i++) {
const currentLetter = str[i];
// if the current letter is a, e, i, o or u
if (vowels[currentLetter]) {
// increase the vowelsCount
vowelsCount++;
}
}
return vowelsCount;
}
function getCount(str) {
const vowels = {
a: true,
e: true,
i: true,
o: true,
u: true
};
return str.split('').reduce((vowelsCount, currentLetter) => {
if (vowels[currentLetter]) {
vowelsCount++;
}
return vowelsCount;
}, 0);
}
function getCount(str) {
const vowels = {
a: true,
e: true,
i: true,
o: true,
u: true
};
return str.split('').filter((currentLetter) => vowels[currentLetter]).length;
}
console.log(getCount("abracadabra") == 5);