const stringConvert = string => {
let splitString = string.split("");
let vowels = splitString.filter(letters => {
return letters === "a", "e", "i", "o", "u"
})
return console.log(vowels);
};
stringConvert("Why don't you try and split this string into vowels, please?");
The code above comes back undefined and I am trying to create a function that takes a string and counts how many vowels are in it. So far I have used the .split() to create an array with each individual character of the string and then I tried using .filter() to filter out each character that is a vowel.
What (only vanilla javascript) would allow me to filter out each vowel from an array, so then I could use .length on that array to get the number of vowels?
const stringConvert = string => {
let splitString = string.split("");
let vowels = ["a", "e", "i", "o", "u"];
let letters = {};
splitString.forEach(l => {
letters[l] = letters[l] ? letters[l] + 1 : 1;
})
let vowelsCount = [];
vowels.forEach(l => vowelsCount[l] = letters[l]);
return vowelsCount;
};
stringConvert("Why don't you try and split this string into vowels, please?");