Files
30-seconds-of-code/snippets/palindrome.md
2022-05-14 15:55:07 +03:00

746 B

title, tags, expertise, cover, firstSeen, lastUpdated
title tags expertise cover firstSeen lastUpdated
Palindrome string intermediate blog_images/bridge-drop.jpg 2017-12-17T16:41:31+02:00 2020-10-22T20:24:04+03:00

Checks if the given string is a palindrome.

  • Normalize the string to String.prototype.toLowerCase() and use String.prototype.replace() to remove non-alphanumeric characters from it.
  • Use the spread operator (...) to split the normalized string into individual characters.
  • Use Array.prototype.reverse(), String.prototype.join() and compare the result to the normalized string.
const palindrome = str => {
  const s = str.toLowerCase().replace(/[\W_]/g, '');
  return s === [...s].reverse().join('');
};
palindrome('taco cat'); // true