DEV Community

Discussion on: JavaScript Katas: Count the number of each character in a string

Collapse
 
theonlybeardedbeast profile image
TheOnlyBeardedBeast

Here is my reduce solution

const result = text.split("").reduce((acc,val)=>{
  acc[val]=(acc[val]||0)+1;
  return acc;
},{})
Collapse
 
jonrandy profile image
Jon Randy 🎖️

Shorter version:

const result = [...text].reduce(
  (acc,val)=>(acc[val]=~~acc[val]+1,acc), {}
)
Collapse
 
theonlybeardedbeast profile image
TheOnlyBeardedBeast

Nice!