javascript count vowels 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: vowels consonants javascript
function readLetter() {
return prompt("Type a letter: ");
}
function isVowel(character) {
switch (character) {
case "a":
case "A":
case "e":
case "E":
case "i":
case "I":
case "o":
case "O":
case "u":
case "U":
return true;
default:
return false;
}
}
var letter = readLetter();
if (isVowel(letter)) alert("The letter is a vowel.");
else alert("The letter is consonant.");