how to count vowels in a string in javascript code example
Example 1: count vowels in javascript
const vowels = ['a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U'];
function countVowels(sentence) {
let counts = 0;
for(let i = 0; i < vowels.length; i++) {
if(vowels.includes(sentence[i])) {
counts++;
}
}
return console.log(counts);
}
countVowels('Hello World');
countVowels('AaEeIiOoUu');
countVowels('aaaaa');
Example 2: how to make a vowel counter in javascript
function getCount(str) {
let vowelList = 'AEIOUaeiou'
let vowelsCount = 0;
for(var i = 0; i < str.length ; i++)
{
if (vowelList.indexOf(str[i]) !== -1)
{
vowelsCount += 1;
}
}
return vowelsCount;
}