functioncountVowels(string){constvowels='aeiouAEIOU';returnstring.split('').reduce((counter,current)=>{if(vowels.indexOf(current)!=-1){if(counter[current]){counter[current]+=1;}else{counter[current]=1;}}returncounter;},{});}console.log(countVowels('How much wood would a woodchuck chuck if a woodchuck could chuck wood?'));// { o: 11, u: 7, a: 2, i: 1 }
We're a place where coders share, stay up-to-date and grow their careers.
JavaScript, using reduce: