DEV Community

Justin
Justin

Posted on • Edited on

JS Challenge: Check if a string is a palindrome

Any string, after removing whitespaces and punctuations, that reads the same when the order of letters is reversed - regardless of the case of letters - is considered a palindrome. For example:

'Was it a car or a cat I saw?'

So the steps involved are:

  1. Convert the string to uniform case
  2. Remove all non-alphabet characters from the string
  3. Reverse the order of letters and check if it reads the same as the original string
const str = "Was it a car or a cat I saw?";
[...str.toLowerCase().replace(/[^a-z]/g, '')].join('') ===
 [...str.toLowerCase().replace(/[^a-z]/g, '')].reverse().join('')
Enter fullscreen mode Exit fullscreen mode

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay