DEV Community

Discussion on: JavaScript Katas: Split a number array into odd and even numbers

Collapse
theonlybeardedbeast profile image
TheOnlyBeardedBeast

Or a simple reduce for the functional way

numbers.reduce((acc,val)=>{
return (val%2===1 ? {...acc,odd:[...acc.odd,val]}:{...acc,even:[...acc.even,val]});
},{odd:[],even:[]})

Collapse
sqlrob profile image
Robert Myers

Yeah, I would've gone with the reduce too, since that's only one iteration over the array, not two. Not sure I'd use spread though, wouldn't that kill the gain?

Collapse
theonlybeardedbeast profile image
TheOnlyBeardedBeast

Probably it would, so just mutate the acc and return it, I think even after all the solutions, I think the simple for loop performs the best.

Collapse
miku86 profile image
miku86 Author

Hey there,

thanks for your solution!
Nice opportunity for people who want to step up their reduce-skills.

Collapse
monicamakes profile image
Monica

I was just going to comment, that this would also be done with reduce (my favorite)