Write an isEven
function to check if a number is even
without using the modulus
operator.
const isEven = (n) => ...
isEven(2) // => true
isEven('127') // => false
isEven('12abc2') // => false
Write an isEven
function to check if a number is even
without using the modulus
operator.
const isEven = (n) => ...
isEven(2) // => true
isEven('127') // => false
isEven('12abc2') // => false
For further actions, you may consider blocking this person and/or reporting abuse
Joseph Abraham -
Marcus Kohlberg -
Prashant Sharma -
Romina Mendez -
Top comments (10)
One liner solution :
Solved!
Instead of doing
Try
Without convert to Number
another solution:
isEven = (num) => Number.isInteger(num) && (!(num & 1))
Can anyone solve it without converting into a number?
I don't know if that's possible. I mean, using the
parseInt
function could work if someone inputs a string.A Little help, you can use a regexp
const isEven = (n) => !!n.toString().match("^[0-9]*[02468]$");