Created
October 4, 2013 15:00
-
-
Save CarlinCanales/6827426 to your computer and use it in GitHub Desktop.
Another challenge from another developer. This will verify if the string passed is a palindrome. Check out http://en.wikipedia.org/wiki/Palindrome for more details on what a palindrome is. The best test for this is Demetri Martin's Palindrome poem (which is one of the console.logs). It will strip all non-word characters, numbers and underscores …
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 isPalindrome(str){ | |
var iteration, i = 0; | |
// if str is only one character long, no matter the character, return false | |
if(str.length < 2) return false; | |
// set all characters to lowercase and strip any invalid characters | |
str = str.toLowerCase().replace(/[\W\d_]/g, ""); | |
// if string length is odd strip out middle character | |
if(str.length % 2 !== 0){ | |
// convert string to array of characters | |
str = str.split(""); | |
// remove the middle character from the array | |
str.splice((str.length-1)/2,1); | |
// convert array back to string | |
str = str.join(""); | |
} | |
// cut length in half to determine how many times to iterate over string | |
iteration = str.length/2; | |
// iterate over string comparing the bookended characters from the outside in | |
for(i; i < iteration; i++){ | |
// if characters don't match at any point return false | |
if(str[i] !== str[(str.length-1)-i]){ | |
return false; | |
} | |
} | |
// at this point all characters have matched so return true | |
return true; | |
} | |
//console.log(isPalindrome("no 'x' 120044()[]\"{}<>\/$~^|-+%*=!?#&@_:;.,''innixon")) | |
//console.log(isPalindrome("no \"x\" in nix on")); | |
//console.log(isPalindrome("anna")); | |
//console.log(isPalindrome("annabannana")); | |
//console.log(isPalindrome("noxinnixon")); | |
//console.log(isPalindrome("rececer")); | |
//console.log(isPalindrome("RaCeCaR")); | |
//console.log(isPalindrome("rac3car")); | |
//console.log(isPalindrome("rac_car")); | |
//console.log(isPalindrome("$an$na$")); | |
//console.log(isPalindrome("bb")); | |
//console.log(isPalindrome("a")); | |
//console.log(isPalindrome("Amore, Romai.")); | |
//console.log(isPalindrome("A man, a plan, a canal: Panama.")); | |
//console.log(isPalindrome("Dammit I'm mad. Evil is a deed as I live. God, am I reviled? I rise, my bed on a sun, I melt. To be not one man emanating is sad. I piss. Alas, it is so late. Who stops to help? Man, it is hot. I'm in it. I tell. I am not a devil. I level \"Mad Dog\". Ah, say burning is, as a deified gulp, In my halo of a mired rum tin. I erase many men. Oh, to be man, a sin. Is evil in a clam? In a trap? No. It is open. On it I was stuck. Rats peed on hope. Elsewhere dips a web. Be still if I fill its ebb. Ew, a spider… eh? We sleep. Oh no! Deep, stark cuts saw it in one position. Part animal, can I live? Sin is a name. Both, one… my names are in it. Murder? I'm a fool. A hymn I plug, deified as a sign in ruby ash, A Goddam level I lived at. On mail let it in. I'm it. Oh, sit in ample hot spots. Oh wet! A loss it is alas (sip). I'd assign it a name. Name not one bottle minus an ode by me: \"Sir, I deliver. I'm a dog\" Evil is a deed as I live. Dammit I'm mad.")); | |
//console.log(isPalindrome("A Toyota's a Toyota.")); | |
//console.log(isPalindrome("wow")); | |
//console.log(isPalindrome("Never odd or even")); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment