DEV Community

Discussion on: Daily Challenge #35 - Find the Outlier

Collapse
 
wheatup profile image
Hao • Edited

There's a potential bug.

-21 will fail both isOdd and isEven function.

The function isOdd should be

const isOdd = n => n % 2 !== 0

Since negative odd numbers mod 2 equals to -1 instead of 1.

Collapse
 
dance2die profile image
Sung M. Kim • Edited

Whoa.... you've just caught years worth of bugs I have possibly created... 😅

Thank you @Hao for finding the issue. 😀
I really didn't think that thru. 👍

Here is the updated code

let isEven = n => n % 2 === 0
let isOdd = n => n % 2 !== 0
const findOutlier = arr => {
    const odds = arr.filter(isOdd)
    const evens = arr.filter(isEven)
    return odds.length < evens.length ? odds[0] : evens[0]
}
[
  [2, 4, 0, 100, 4, 11, 2602, 36],
  [160, 3, 1719, 19, 11, 13, -21],
  [4, 8, 15, 16, 24, 42],
  [16, 6, 40, 66, 68, 28],
  [-21, 1, 2, 3]
].map(findOutlier)

updated result

I think I would probably use !isEven next time 😎