I want to get the count of the vowels on the string using reduce() method of javascript. The below is the code and the problem is that the value of a, e, i, o, u after the destructuring the acc is coming as undefined.
const str = 'I am just a string. I mean nothing serious and I am being used to count the number of vowels I have.';
const strArr = str.split('');
const mapVowels = strArr.reduce(countVowels, {});
function countVowels(acc, char) {
console.log(char)
var { a = 0, e = 0, i = 0, o = 0, u = 0 } = acc;
if (char === "a") {
return {...acc, a: a + 1};
}
if (char === 'i') {
return { ...acc, i: i + 1}
}
if (char === 'e') {
return { ...acc, e: e + 1}
}
if (char === 'o') {
return { ...acc, o: o + 1}
}
if (char === 'u') {
return { ...acc, u: u + 1}
}
}
console.log(mapVowels)
I want mapVowels to be an object with keys a, e, i, o,u and value the number of time they are repeating in the str.