forked from knaxus/problem-solving-javascript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
40 lines (34 loc) · 970 Bytes
/
index.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
const pattern = /[^\w]/g;
const cleanString = str => str.replace(pattern, '').toLowerCase();
const isVowel = char => char === 'a' || char === 'e' || char === 'i' || char === 'o' || char === 'u';
function countVowelsItteratively(str) {
const cleanedString = cleanString(str);
let count = 0;
for (let i = 0; i < cleanedString.length; i += 1) {
if (isVowel(cleanedString[i])) {
count += 1;
}
}
return count;
}
function countVowelsItterativelyES6(str) {
const cleanedString = cleanString(str);
const vowels = ['a', 'e', 'i', 'o', 'u'];
let count = 0;
// eslint-disable-next-line no-restricted-syntax
for (const char of cleanedString) {
if (vowels.includes(char)) {
count += 1;
}
}
return count;
}
function countVowelsUsingRegex(str) {
const match = str.match(/[aeiou]/gi);
return match ? match.length : 0;
}
module.exports = {
countVowelsItteratively,
countVowelsItterativelyES6,
countVowelsUsingRegex,
};