How to determine if a number is odd in JavaScript

后端 未结 27 1772
一向
一向 2020-11-27 10:05

Can anyone point me to some code to determine if a number in JavaScript is even or odd?

27条回答
  •  春和景丽
    2020-11-27 10:54

    A more functional approach in modern javascript:

    const NUMBERS = "nul one two three four five six seven ocho nueve".split(" ")
    
    const negate = f=> (...args)=> !f(...args)
    const isOdd  = n=> NUMBERS[n % 10].indexOf("e")!=-1
    const isEven = negate(isOdd)
    

提交回复
热议问题