## DEV Community is a community of 670,230 amazing developers

We're a place where coders share, stay up-to-date and grow their careers.

# Can you hack this? #2

Alfredo Salzillo 🐺
🐺

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
``````

## Discussion (10)

Jonathan ARNAULT • Edited

One liner solution :

``````const isEven = (n) => (+n && (n & 1)) === 0
``````
PyBash

Solved!

``````const isEven = (n) => {
q = n/2
ans = q===parseInt(q)
if (ans==true) {
return true
}
else {
return false
}
}

console.log(isEven(2)) // => true
console.log(isEven(127)) // => false
console.log(isEven('127')) // => false
console.log(isEven('12abc2')) // => false
``````
𝐋𝐄𝑽𝐈𝑨𝐓𝐇𝐀𝐍 Programming

``````if (ans==true) {
return true
}
else {
return false
}
``````

Try

``````return ans == true;
``````
Omicron
``````isEven=n=>!!(+n+1<<31)
``````
Vinicius Cainelli • Edited

Without convert to Number

``````const isEven = (n) => {
const rgx = new RegExp('^\d*[02468]\$')
return rgx.test(n)
};
``````
Ido David

another solution:
isEven = (num) => Number.isInteger(num) && (!(num & 1))

Alfredo Salzillo 🐺

Can anyone solve it without converting into a number?

𝐋𝐄𝑽𝐈𝑨𝐓𝐇𝐀𝐍 Programming

I don't know if that's possible. I mean, using the `parseInt` function could work if someone inputs a string.

Alfredo Salzillo 🐺

A Little help, you can use a regexp

Angelo Caci

const isEven = (n) => !!n.toString().match("^[0-9]*[02468]\$");