js print vowels from string 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: count vowels in a string javascript
const countVowels = (str) => (str.match(/[aeiou]/gi) || []).length
Example 3: javascript loop over the alphabet and return the vowels
function vowelsAndConsonants(s) {
const vowels = ["a","e","i","o","u"];
const arr = s.split("");
var vowelsFound = [];
var cons = [];
for (var i in arr) {
if (vowels.includes(arr[i])) {
vowelsFound.push(arr[i]);
} else {
cons.push(arr[i]);
}
}
console.log(vowelsFound.join('\n') + '\n' + cons.join('\n'))
}
vowelsAndConsonants(javascriptloops);