JavaScript Katas: Split a number array into odd and even numbers miku86 on July 09, 2020 Intro 🌐 Today, I start a new series about code katas. I will take interesting katas of all levels and explain how to solve them. Probl... Read full post Collapse Expand Abe Dolinger Abe Dolinger Abe Dolinger Follow Frontend / React Native dev Location Seattle Education Flatiron School Pronouns He/him Work Mobile Engineer @ Treecard Joined Apr 5, 2019 • Jul 9 '20 Dropdown menu Copy link Hide const splitNumbers = numbers => { const oddsAndEvens = { odds: [], evens: [] }; numbers.forEach(number => { oddsAndEvens[number % 2 === 0 ? 'evens' : 'odds'] .push(number); }; return oddsAndEvens; }; Collapse Expand miku86 miku86 miku86 Follow Mentor & Senior Web Developer - I help people to reach their (career) goals. => https://miku86.com Location Germany Joined Nov 22, 2017 • Jul 10 '20 Dropdown menu Copy link Hide Hi Abe, nice solution, thanks! Collapse Expand Abe Dolinger Abe Dolinger Abe Dolinger Follow Frontend / React Native dev Location Seattle Education Flatiron School Pronouns He/him Work Mobile Engineer @ Treecard Joined Apr 5, 2019 • Jul 10 '20 Dropdown menu Copy link Hide All credit to this post! codereview.stackexchange.com/a/162... Been using this at work and loving it. Collapse Expand pentacular pentacular pentacular Follow Joined Jul 8, 2020 • Jul 10 '20 • Edited on Jul 10 • Edited Dropdown menu Copy link Hide The problem with katas is that they ignore the 'why' of the problem. Since we don't know why we're doing this, let's delegate that problem to the caller as much as possible. This is really a classification problem. const splitNumber = (numbers, emitEven, emitOdd) => numbers.forEach(n => (n % 2 ? emitOdd : emitEven)(number)); Now we can defer the arrangement of the classified numbers to someone else. If they want to store them in arrays, then they can do that. const even = []; const odd = []; splitNumber(numbers, n => even.push(n), n => odd.push(n)); But that's their problem. Collapse Expand Julián Chamalé Julián Chamalé Julián Chamalé Follow Joined Oct 5, 2019 • Jul 10 '20 Dropdown menu Copy link Hide Nice analysis Collapse Expand miku86 miku86 miku86 Follow Mentor & Senior Web Developer - I help people to reach their (career) goals. => https://miku86.com Location Germany Joined Nov 22, 2017 • Jul 10 '20 Dropdown menu Copy link Hide Hi, thanks for your insights, interesting question to think about! Collapse Expand TheOnlyBeardedBeast TheOnlyBeardedBeast TheOnlyBeardedBeast Follow .NET Core + TypeScript + React + Flutter + UWP Yep, that's me. Location Slovakia Work Team lead, Full stack dev Joined Nov 12, 2019 • Jul 9 '20 Dropdown menu Copy link Hide 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 Expand Monica Monica Monica Follow Full time project engineer, part time student of web development, computer science and the world! Location Houston TX Education Cornell University Work Engineer Joined Jan 13, 2020 • Jul 9 '20 Dropdown menu Copy link Hide I was just going to comment, that this would also be done with reduce (my favorite) Collapse Expand miku86 miku86 miku86 Follow Mentor & Senior Web Developer - I help people to reach their (career) goals. => https://miku86.com Location Germany Joined Nov 22, 2017 • Jul 10 '20 Dropdown menu Copy link Hide Hey there, thanks for your solution! Nice opportunity for people who want to step up their reduce-skills. Collapse Expand Robert Myers Robert Myers Robert Myers Follow Joined Apr 8, 2018 • Jul 9 '20 Dropdown menu Copy link Hide 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 Expand TheOnlyBeardedBeast TheOnlyBeardedBeast TheOnlyBeardedBeast Follow .NET Core + TypeScript + React + Flutter + UWP Yep, that's me. Location Slovakia Work Team lead, Full stack dev Joined Nov 12, 2019 • Jul 9 '20 Dropdown menu Copy link Hide 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. Code of Conduct • Report abuse For further actions, you may consider blocking this person and/or reporting abuse
Hi Abe,
nice solution, thanks!
All credit to this post! codereview.stackexchange.com/a/162... Been using this at work and loving it.
The problem with katas is that they ignore the 'why' of the problem.
Since we don't know why we're doing this, let's delegate that problem to the caller as much as possible.
This is really a classification problem.
Now we can defer the arrangement of the classified numbers to someone else.
If they want to store them in arrays, then they can do that.
But that's their problem.
Nice analysis
Hi,
thanks for your insights,
interesting question to think about!
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:[]})
I was just going to comment, that this would also be done with reduce (my favorite)
Hey there,
thanks for your solution!
Nice opportunity for people who want to step up their
reduce
-skills.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?
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.