DEV Community

loading...

Palindrome Checker

2spacemilk profile image Mark Harless Updated on ・3 min read

I'm going back to the basics with Free Code Camp! I believe it's a neat website for people who are interested in coding or have some experience. One of the projects found in their JavaScript and Data Structures is a Palindrome Checker.

Here is the problem:

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.

You'll need to remove all non-alphanumeric characters (punctuation, spaces and symbols) and turn everything into the same case (lower or upper case) in order to check for palindromes.

We'll pass strings with varying formats, such as "racecar", "RaceCar", and "race CAR" among others.

We'll also pass strings with special symbols, such as "2A3*3a2", "2A3 3a2", and "2_A3*3#A2".

And this is what we're given to work with:

function palindrome(str) {
  return true;
}

palindrome("eye");

Enter fullscreen mode Exit fullscreen mode

I much prefer ES6 syntax so let's do that real quick!

const palindrome = (str) => {
  return true;
}

palindrome("eye");
Enter fullscreen mode Exit fullscreen mode

The first thing I like to do when confronting a problem is reading the instructions twice (at least). Breaking it down we need to check if a string is a palindrome — ignoring punctuation, case and spacing. Right off the bat, this tells me I need to use REGEX, ugh.

Let's first tackle this problem by having the function work correctly using a single word as an argument.

const palindrome = (str) => {
  const check = str.split('').reverse().join('')

  return str === check
}

palindrome("eye");
// true
Enter fullscreen mode Exit fullscreen mode

I created a constant check that stores the string argument after several string methods are performed on it. split breaks up all of the characters and puts them in an array, reverse reverses the array, and join combines the array back into a string. Then the return statement checks to see if the two variables match and spits out the appropriate boolean.

It works as expected and returns true when the argument "eye" is passed into the function. Great! But what if I passed in "Eye" with a capital "E". Surely "Eye" is not the same as "eye" so it will return false but it is still a palindrome! Let's fix that real quick.

const palindrome = (str) => {
  const string = str.toLowerCase()
  const check = string.split('').reverse().join('')

  return string === check
}

palindrome("Eye");
// true
Enter fullscreen mode Exit fullscreen mode

I added a line at the top which creates the string constant. All it does is store the argument str after we have performed the toLowerCase function on it. Now, at the end of our function, we are comparing string and check (not to be confused with str) and it works perfectly.

Lastly, we should use Regular Expressions (REGEX) to match patterns with our string. We're doing this to remove everything that's not an alphanumeric character. This includes whitespaces and special characters such as !@#$%^&*().

We do this using the replace method:

const palindrome = str => {
  const string = str.toLowerCase().replace(/\W|_/g, '');
  const check = string.split('').reverse().join('');

  return string === check;
};

palindrome('0_0 (: /- :) 0-0');
// true
Enter fullscreen mode Exit fullscreen mode

REGEX is a bit of a complicated topic to cover in this blog post so I'm just going to break down how I used the replace method. After turning the string into all lowercase, I'm replacing everything that's not a letter, number or underscore with ''. The first part of the REGEX is \W which finds everything that's not a letter or a number (yes, this includes whitespaces). Strangely enough, this includes all special characters except for the underscore. So, the second part of my regex is |\_. The | means or and \_ means underscore. In the end, in plain English, we're saying remove everything that is not a letter, a number or an underscore.

You can see all of my notes on REGEX here. I've also included a great online resource to test your REGEX patterns.

And that's it! REGEX is an important skill to master in JavaScript. I think it's a tough tackle because it doesn't follow traditional JavaScript syntax. I'll be back next week to keep doing more Algorithms so please smash that "like" button and hit "subscribe"!

Discussion

pic
Editor guide