Miniature checker if a number is a palindrome (http://en.wikipedia.org/wiki/Palindrome)
-
-
Save atk/36a232de098adfc510ed to your computer and use it in GitHub Desktop.
Number is Palindrome
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
function( | |
n // number | |
) { | |
n = (n + '').split(''); // coerce to string, split into array | |
return '' + n == n.reverse() // compare the joined array with a reversed version; | |
// string coercion is required to ensure we don't compare object references | |
// using a string on one side of the comparison coerces the other side to string, too. | |
} |
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
function(n){n=(n+'').split('');return''+n==n.reverse()} |
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
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE | |
Version 2, December 2004 | |
Copyright (C) 2015 Alex Lohr (formerly Kloss) <alexthkloss@web.de> | |
Everyone is permitted to copy and distribute verbatim or modified | |
copies of this license document, and changing it is allowed as long | |
as the name is changed. | |
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE | |
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION | |
0. You just DO WHAT THE FUCK YOU WANT TO. |
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
{ | |
"name": "NumberIsPalindrome", | |
"description": "Checks whether a given number is a palindrome.", | |
"keywords": [ | |
"palindrome", | |
"number", | |
"math", | |
"euler" | |
] | |
} |
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
<!DOCTYPE html> | |
<title>numberIsPalindrome</title> | |
<div>Expected value: <b>true,true,false,false</b></div> | |
<div>Actual value: <b id="ret"></b></div> | |
<script> | |
// write a small example that shows off the API for your example | |
// and tests it in one fell swoop. | |
var numberIsPalindrome = function(n){n=(n+'').split('');return''+n==n.reverse()} | |
document.getElementById( "ret" ).innerHTML = [numberIsPalindrome(123321), numberIsPalindrome(123454321), | |
numberIsPalindrome(123123), numberIsPalindrome(11178111)] | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment