Files
30-seconds-of-code/snippets/palindrome.md
mphan6 8dcaaf4c63 Correct palindrome.md description
Corrected the second toLowerCase() function typo and tried to make the
description more clear.
2018-10-14 17:47:48 -04:00

632 B

palindrome

Returns true if the given string is a palindrome, false otherwise.

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

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