Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

count the vowels in a string in javascript

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.

like image 454
Prabs Avatar asked Feb 12 '26 06:02

Prabs


2 Answers

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)
like image 88
Ori Drori Avatar answered Feb 16 '26 02:02

Ori Drori


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);
like image 38
Andy Avatar answered Feb 16 '26 02:02

Andy



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!