DEV Community

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

Collapse
 
pentacular profile image
pentacular • Edited

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
 
d4rthv4d3r profile image
Julián Chamalé

Nice analysis

Collapse
 
miku86 profile image
miku86

Hi,

thanks for your insights,
interesting question to think about!