Files
30-seconds-of-code/snippets/Check_for_palindrome.md
Angelos Chalaris 30b9b1b522 Palindrome updated
2017-12-12 18:08:03 +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