Skip to content

Instantly share code, notes, and snippets.

@mjmeilahn
Last active May 29, 2020 15:46
Show Gist options
  • Save mjmeilahn/773952e612570ebd3f823f82ffc62e2d to your computer and use it in GitHub Desktop.
Save mjmeilahn/773952e612570ebd3f823f82ffc62e2d to your computer and use it in GitHub Desktop.
JS: Palindrome Function
// Takes a single string and returns a boolean whether it is the same forwards or backwards
// EXAMPLE:
// "oscar" -> false
// "aaaa" -> true
// "Racecar" -> true
// "Madam, I'm Adam" -> true
// Args: string
// Return: boolean
const palindrome = str => {
const clean = str.toLowerCase().replace(/( )|[.,'\/#!$%\^&\*;:{}=\-_`~\(\)]/g, "");
const final = clean.split('').reverse().join('');
return clean === final;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment