Check for Palindromes in JavaScript

Sonya Moisset
iDevOI
Published in
3 min readNov 30, 2016

This article is based on Free Code Camp Basic Algorithm Scripting “Check for Palindromes”.

A palindrome is a word, phrase, number, or other sequence of characters which reads the same backward or forward. The word “palindrome” was first coined by the English playwright Ben Jonson in the 17th century, from the Greek roots palin (“again”) and dromos (“way, direction”). — src. Wikipedia

Algorithm Challenge

Return true if the given string is a palindrome. Otherwise, return false.

A palindrome is a word or sentence that’s spelled the same way both forward and backward, ignoring punctuation, case, and spacing.

Note. You’ll need to remove all non-alphanumeric characters (punctuation, spaces and symbols) and turn everything lower case in order to check for palindromes.

We’ll pass strings with varying formats, such as “racecar”, “RaceCar”, and “race CAR” among others.

Provided test cases

palindrome(“race car”) should return truepalindrome(“not a palindrome”) should return falsepalindrome(“A man, a plan, a canal. Panama”) should return truepalindrome(“never odd or even”) should return truepalindrome(“nope”) should return falsepalindrome(“almostomla”) should return falsepalindrome(“My age is 0, 0 si ega ym.”) should return truepalindrome(“1 eye for of 1 eye.”) should return falsepalindrome(“0_0 (: /-\ :) 0–0”) should return true

Which Regular Expression will we need to pass the last test case?

Regular expressions are patterns used to match character combinations in strings.

When the search for a match requires something more than a direct match, the pattern includes special characters.

To pass the last test case, we can use two Regular Expressions:/[^A-Za-z0–9]/g  or/[\W_]/g

\W removes all non-alphanumeric characters:

  • \W matches any non-word character
  • \W is equivalent to [^A-Za-z0–9_]
  • \W matches anything that is not enclosed in the brackets

What does that mean?

[^A-Z] matches anything that is not enclosed between A and Z[^a-z] matches anything that is not enclosed between a and z[^0-9] matches anything that is not enclosed between 0 and 9[^_] matches anything that does not enclose _

But in our test case, we need palindrome(“0_0 (: /-\ :) 0–0”) to return true, which means “_(: /-\ :)–” has to be matched.

We will need to add “_” to pass this specific test case.

We now have “\W_

We will also need to add the g flag for global search.

We finally have “/[\W_]/g

/[\W_]/g was used for pure demonstrative purpose to show how RegExp works. /[^A-Za-z0–9]/g is the easiest RegExp to choose.

Check for Palindromes With Built-In Functions — with replace() and reverse()

For this solution, you will use several methods:

  • The toLowerCase() method to return the calling string value converted to lowercase.
  • The replace() method to return a new string with some or all matches of a pattern replaced by a replacement. We will use one of the RegExp we just created earlier.
  • The split() method splits a String object into an array of strings by separating the string into sub strings.
  • The reverse() method reverses an array in place. The first array element becomes the last and the last becomes the first.
  • The join() method joins all elements of an array into a string.

Here’s my solution, with embedded comments:

Here’s my solution, without comments:

I hope you found this helpful. This is part of my “How to Solve FCC Algorithms” series of articles on the Free Code Camp Algorithm Challenges, where I propose several solutions and explain step-by-step what happens under the hood.

You can follow me on Medium, Twitter, Github and LinkedIn, right after you click the green heart below ;-)

‪#‎StayCurious‬, ‪#‎KeepOnHacking‬ & ‪#‎MakeItHappen‬!

--

--

Sonya Moisset
iDevOI

Senior Security 🥑 || GitHub 🌟 || ☁️ OpenUK Ambassador || 🎓 CAPSLOCK & CyberGirls Lead Mentor || 👩🏻‍💻 Epic Women in Cyber/Tech initiatives