Last active
February 28, 2017 19:02
-
-
Save mikepenzin/d5cf5060dfe4f95dc5af69945406e392 to your computer and use it in GitHub Desktop.
Function isInteger(x) that determines if x is an integer.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// isInteger(3); // Outputs true | |
// isInteger("5"); // Outputs false | |
// isInteger("car"); // Outputs false | |
function isInteger(x) { | |
// ^ stands for XOR so every integer with (4^0) will give integer itself. Every string will give 0 | |
return (x^0) === x; | |
} | |
// Another solution | |
function isInteger(x){ | |
return Math.round(x) == x; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment