We are required to write a JavaScript function that takes in a string that might contain some alphabets. The function should count and return the number of vowels that exists in the string.
Example
Following is the code −
const str = 'this is a string';
const countVowels = (str = '') => {
str = str.toLowerCase();
const legend = 'aeiou';
let count = 0;
for(let i = 0; i < str.length; i++){
const el = str[i];
if(!legend.includes(el)){
continue;
};
count++;
};
return count;
};
console.log(countVowels(str));Output
Following is the output on console −
4