Reputation: 127
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.
Upvotes: 3
Views: 2501
Reputation: 386883
This approach take the wanted characters, builds an object for counting and takes a check and count the character, if in the object.
const
vowels = 'aieou';
str = 'I am just a string. I mean nothing serious and I am being used to count the number of vowels I have.';
mapVowels = Array
.from(str.toLowerCase())
.reduce(
(acc, char) => (char in acc && acc[char]++, acc),
Object.assign(...Array.from(vowels, c => ({ [c]: 0 })))
);
console.log(mapVowels);
Upvotes: 0
Reputation: 193358
When a non vowel character is found, you don't return acc
. So acc
is undefined
on the next iteration, and the destructuring fails. Return acc
even if it's not a vowel:
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) {
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}
}
return acc;
}
console.log(mapVowels)
In addition, you can make the code more DRY by creating an array or a string of vowels, and using String.includes()
or Array.inclues()
to identify vowels:
const vowels = 'aieou';
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) {
if(!vowels.includes(char)) return acc;
const { [char]: val = 0 } = acc;
return {...acc, [char]: val + 1};
}
console.log(mapVowels)
Upvotes: 5
Reputation: 5941
You could do something like this:
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 VOWELS = ['a', 'e', 'i', 'o', 'u', 'y'];
var count = str.trim().split('').reduce((accum, el) => {
if (VOWELS.indexOf(el) > -1) {
accum[el] += 1;
}
return accum;
}, {
'a': 0,
'e': 0,
'i': 0,
'o': 0,
'u': 0,
'y': 0
});
console.log(count);
Upvotes: 0
Reputation: 63587
Ori has you covered on the reason your code doesn't work. You're also duplicating a lot of code, so here's a reduce
example that allows you to build up the counts in one line.
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 vowelRe = /[aeiou]/;
const countVowels = str.split('').reduce((acc, c) => {
// Test if the current letter is a vowel
const found = vowelRe.test(c);
// If it is: if the vowel exists in the object as a key increase the count,
// and if it doesn't set it to zero, and then increase the count
if (found) acc[c] = (acc[c] || 0) + 1;
// return the accumulator
return acc;
}, {});
console.log(countVowels);
Upvotes: 4