Last active
December 10, 2015 12:52
-
-
Save Stoffo/38f8b64ee5b877a6a7da to your computer and use it in GitHub Desktop.
Checks if String is a palindrome.
This file contains hidden or 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 palindrome(str) { | |
// Convert to lower case and replace everything what's not a letter or a number. | |
var x = str.toLowerCase().replace(/[^a-z0-9]/g, ''); | |
//convert string to array, turn it around and put it back together an match it against filtered string: | |
return x.split('').reverse().join('') === x; | |
} | |
palindrome("eye"); | |
/* | |
palindrome("eye") should return a boolean. | |
palindrome("eye") should return true. | |
palindrome("race car") should return true. | |
palindrome("not a palindrome") should return false. | |
palindrome("A man, a plan, a canal. Panama") should return true. | |
palindrome("never odd or even") should return true. | |
palindrome("nope") should return false. | |
palindrome("almostomla") should return false. | |
palindrome("My age is 0, 0 si ega ym.") should return true. | |
palindrome("1 eye for of 1 eye.") should return false. | |
palindrome("0_0 (: /-\ :) 0-0") should return true. | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment