Skip to content

Instantly share code, notes, and snippets.

@rurtubia
Created December 19, 2015 11:47
Show Gist options
  • Save rurtubia/8381509420f461ee191e to your computer and use it in GitHub Desktop.
Save rurtubia/8381509420f461ee191e to your computer and use it in GitHub Desktop.
My solution to FreeCodeCamp bonfire number 4
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