Files
30-seconds-of-code/snippets/check-for-palindrome.md
Angelos Chalaris 323212ce71 Updated filename
2017-12-12 19:07:42 +02:00

465 B

Check for palindrome

Convert string toLowerCase() and use replace() to remove non-alphanumeric characters from it. Then, split('') into individual characters, reverse(), join('') and compare to the original, unreversed string, after converting it tolowerCase().

const palindrome = str =>
  str.toLowerCase().replace(/[\W_]/g,'').split('').reverse().join('') === str.toLowerCase().replace(/[\W_]/g,'');
// palindrome('taco cat') -> true