Menu

gitpiper

palindrome javascript Code Snippet in 2022

stringintermediate

Last Updated: 26 November 2022

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

javascript snippet similar to palindrome For You in November 2022

Subscribe to our Newsletter

Subscribe to get resources directly to your inbox. You won't receive any spam! ✌️

© 2022 GitPiper. All rights reserved

Rackpiper Technology Inc

Company

About UsBlogContact

Subscribe to our Newsletter

Subscribe to get resources directly to your inbox. You won't receive any spam! ✌️