Created
December 19, 2015 11:47
-
-
Save rurtubia/8381509420f461ee191e to your computer and use it in GitHub Desktop.
My solution to FreeCodeCamp bonfire number 4
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 palindrome(str) { | |
//converts everything to lower case | |
str = str.toLowerCase(); | |
//eliminates spaces from string | |
str = str.replace(/ /g,''); | |
//removes: '/', '_', '\', '(', ')', '.', ',', '-' from the string | |
//'-' must be declared at the end of the string | |
str = str.replace(/[/_\\{()}.,-]/g, ''); | |
//stores the string for future comparisons as a new variable | |
var str2 = str; | |
//splits the string into an array | |
var tempArray = str.split(''); | |
//reverses the elements of the array | |
tempArray = tempArray.reverse(); | |
//joins the array elements into a string | |
str = tempArray.join(''); | |
//Compares the stored string with the new one | |
if(str2 === str) | |
return true; | |
else | |
return false; | |
} | |
palindrome("something"); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment