I have an array of random integers like this: [ 1, 2, 3, 4, 0 ], I need to return a new array of either the odd or the even numbers depending on a wanted condition.
I tried the following filter method:
const result = array.filter(item => {
const test = item % 2;
if(wanted === 'odd' && test > 0) return item;
if(wanted === 'even' && test === 0) return item;
})
and had expected this to work but it only ever returns [2, 4] and never [0, 2, 4] when the wanted is even.
Any thoughts on why this might be the case would be greatly appreciated.
testwill always be either1or0. Testing> 0is confusing when you're really just looking for1.