DEV Community

Discussion on: JavaScript Programming Problem 2

Collapse
 
pengeszikra profile image
Peter Vivo

I learn from wise guy: fun-fun-function reduce is a swish knife of functional programming, in this case:

[..."3123dasds JJKH e o a eee iJ"].reduce( 
 (coll, chr) => coll[chr] 
   ? {...coll, [chr]: coll[chr] + 1} 
   : {...coll, [chr]:1 }
, {});
Enter fullscreen mode Exit fullscreen mode
Collapse
 
hijazi313 profile image
Muhammad Hamza Hijazi

this function is counting all alphabets in string.

Collapse
 
pengeszikra profile image
Peter Vivo

you right!

to work with vowels need filter before this process:


const countChars =  
 (coll, chr) => coll[chr] 
   ? {...coll, [chr]: coll[chr] + 1} 
   : {...coll, [chr]:1 };

const vowels = "aeiouóöőüúűáé"; // hungarian vowels included

const filterWovels = chr => vowels.indexOf(chr) > -1;

const vowelCount = s => [...s.toLowerCase].filter(filterWovels).reduce(countChars, {});
Enter fullscreen mode Exit fullscreen mode