admin管理员组文章数量:1336209
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 ing 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.
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 ing 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.
Share Improve this question edited Jan 20, 2019 at 20:51 Prabs asked Jan 20, 2019 at 20:48 PrabsPrabs 1271 gold badge3 silver badges11 bronze badges 2- 1 Possible duplicate of Counting vowels in javascript – Randy Casburn Commented Jan 20, 2019 at 20:48
- 3 @RandyCasburn Not really. The OP already has code, and the code has a very specific problem that needs to be solved. – Bergi Commented Jan 20, 2019 at 20:52
4 Answers
Reset to default 5When 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)
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);
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);
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);
本文标签: ecmascript 6count the vowels in a string in javascriptStack Overflow
版权声明:本文标题:ecmascript 6 - count the vowels in a string in javascript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742377002a2463361.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论