Alfredo Salzillo Posted on Apr 25, 2021 Can you hack this? #2 #challenge #fun #javascript Can you hack this? (3 Part Series) 1 Can you hack this? #1 2 Can you hack this? #2 3 Can you hack this? #3 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 Enter fullscreen mode Exit fullscreen mode Top comments (10) Subscribe Submit Preview Dismiss Collapse Expand Omicron Omicron Omicron Follow Location France Work Software Engineer Joined Oct 8, 2019 • Apr 25 '21 Dropdown menu Copy link Hide isEven=n=>!!(+n+1<<31) Enter fullscreen mode Exit fullscreen mode Collapse Expand PyBash PyBash PyBash Follow I've been learning to code for around 5 years now. I started with HTML like everybody but eventually landed on Python and I have loved it ever since, I specialize in backend development. Location India Joined Apr 9, 2021 • Apr 25 '21 Dropdown menu Copy link Hide 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 Enter fullscreen mode Exit fullscreen mode Collapse Expand IroncladDev IroncladDev IroncladDev Follow Opinionated Texan software engineer who aims to build and share great software with the world Email conner@ironclad.sh Location Dallas TX USA Education Homeschooled Joined Dec 15, 2020 • Apr 26 '21 Dropdown menu Copy link Hide Instead of doing if (ans==true) { return true } else { return false } Enter fullscreen mode Exit fullscreen mode Try return ans == true; Enter fullscreen mode Exit fullscreen mode Collapse Expand Vinicius Cainelli Vinicius Cainelli Vinicius Cainelli Follow UI Engineer, Front End Developer & Entrepreneur Education PGCert, Web Engineering, SENAC São Paulo, 2015 Work Software Engineer at IKEA.com Joined Jul 5, 2017 • Apr 28 '21 • Edited on Apr 28 • Edited Dropdown menu Copy link Hide Without convert to Number const isEven = (n) => { const rgx = new RegExp('^\d*[02468]$') return rgx.test(n) }; Enter fullscreen mode Exit fullscreen mode Collapse Expand Ido David Ido David Ido David Follow Technological enthusiast and a programmer from a very young age Education B.Sc computer science Joined Feb 23, 2021 • Apr 25 '21 Dropdown menu Copy link Hide another solution: isEven = (num) => Number.isInteger(num) && (!(num & 1)) Collapse Expand Alfredo Salzillo Alfredo Salzillo Alfredo Salzillo Follow 🐺 Location Italy Pronouns He/Him Joined Nov 8, 2018 • Apr 25 '21 Dropdown menu Copy link Hide Can anyone solve it without converting into a number? Collapse Expand IroncladDev IroncladDev IroncladDev Follow Opinionated Texan software engineer who aims to build and share great software with the world Email conner@ironclad.sh Location Dallas TX USA Education Homeschooled Joined Dec 15, 2020 • Apr 26 '21 Dropdown menu Copy link Hide I don't know if that's possible. I mean, using the parseInt function could work if someone inputs a string. Collapse Expand Alfredo Salzillo Alfredo Salzillo Alfredo Salzillo Follow 🐺 Location Italy Pronouns He/Him Joined Nov 8, 2018 • Apr 26 '21 Dropdown menu Copy link Hide A Little help, you can use a regexp Collapse Expand Angelo Caci Angelo Caci Angelo Caci Follow Computer Science, Web Development and Security lover 😁 Location Rome, IT Work Junior Web Developer at Tai Solutions Joined Apr 17, 2021 • Apr 26 '21 Dropdown menu Copy link Hide const isEven = (n) => !!n.toString().match("^[0-9]*[02468]$"); Code of Conduct • Report abuse
Top comments (10)
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
parseIntfunction could work if someone inputs a string.A Little help, you can use a regexp
const isEven = (n) => !!n.toString().match("^[0-9]*[02468]$");