-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathfind-vowels.js
More file actions
47 lines (32 loc) · 1.32 KB
/
find-vowels.js
File metadata and controls
47 lines (32 loc) · 1.32 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
// FIND VOWELS
// Given a string of words or phrases, count the number of vowels.
// We just need to match String with our Regular Expression
// If Matches, we'll count the 'length' property and return it
const countVowel = string => {
// RegExp to Match vowels
const regExp = /[aeiou]/gi;
// Check if Matched or not ! and Return output on console
const matches = string.match(regExp);
return matches ? matches.length : 0;
}
countVowel("I am Shaon Kabir from Bangladesh") // Expected Output:- 10
// Another Way:- using Loop
// Process:-
// we'll store vowels in a container
// for caseSencetivity, we'll create string 'LowerCase'
// we'll convert our string into array removing whiteSpace to iterate a loop on it
// Then we'll Check using 'includes' methods if vowels available or not !
// If vowel found, we'll increase 'countVowel' in every iteration
const findVowels = string => {
let countVowel = 0;
const vowels = ['a', 'e', 'i', 'o', 'u']; // or "aeiou"
const convertedString = string.toLowerCase().split('').filter(space => /\S/.test(space));
console.log(convertedString)
convertedString.forEach(character => {
if(vowels.includes(character)) {
countVowel++;
}
})
return countVowel;
};
findVowels("Hello Bangladesh !") // Expected Output:- 5