The five letters a, e, i, o, u are called vowels. All other alphabets except these 5 vowels are called consonants.
For the input “javascript” str.match(/[aeiou]/gi); gives [“a”, “a”, “i”] * The length property gives the number of vowels present.
//Using inbuilt function
function countVowel(str) {
// find the count of vowels
const count = str.match(/[aeiou]/gi).length;
// return number of vowels
return count;
}
const result = countVowel("javascript");
console.log(result);
//Prints: 3
//Without using inbuilt function
function countVowels(str) {
let count=0;
for(let i=0; i<str.length; i++) {
if(str[i]==="a" || str[i]==="e" || str[i]==="i" || str[i]==="o" || str[i]==="u") {
count+=1;
}
}
return count;
}
console.log(countVowels("hello"));
//Prints: 2