Find Vowel-v1
We write a function that returns the number of vowels used in a string. Vowels are characters ‘a’ ,’e’, ‘i’, ‘o’ and ‘u’
Example:
vowels(‘Hi There!’) –> 3
vowels(‘Why do you ask?’) –> 4
vowels(‘Why?’) –> 0
function vowels(str) {
let count = 0;
const vowelCheker = "aeiou";
for (let char of str.toLowerCase()) {
if (vowelCheker.includes(char)) {
count++;
}
}
return count;
}
We create a function called vowels with parameter called str and variable called count that is equal to 0 and vowelChecker equal to “aeiou”
We loop with for of loop but we make the str parameter toLowerCase() to check even if vowel is uppercased.
Inside the loop we check if vowelChecker.includes(char)
The includes() method determines whether an array includes a certain value among its entries, returning true or false as appropriate. -MDN
If includes returns true we increment count by 1 with count++
In the end we return the count