Skip to content

Instantly share code, notes, and snippets.

@AdrianSkar
Last active February 24, 2018 11:31
Show Gist options
  • Save AdrianSkar/21fc62c37ab467caabb3cb4d40e41132 to your computer and use it in GitHub Desktop.
Save AdrianSkar/21fc62c37ab467caabb3cb4d40e41132 to your computer and use it in GitHub Desktop.
//Requirements: lowercase, non-alphanumeric chars + no underscores
function palindrome(str) {
str = str.toLowerCase();
str = str.replace(/\W/g, ""); //Remove all non-alphanumeric chars
str = str.replace(/_/g, ""); //Remove all underscore chars
// All at once -> str = str.toLowerCase().replace(/\W|_/g, "");
if (str == str.split("").reverse().join('')){ // Check
return true; // If it's a palindrome
}
return false; // If it's not
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment