How to Check For Palindrome in JavaScript

How to Check For Palindrome in JavaScript

Hi there, trust that you are all doing great. In this article, I will be showing you how to check if a word is a palindrome. Trust me, you should be able to follow along if you have a working knowledge of JavaScript. I had solved the Freecodecamp palindrome checker. But, It's my habit to re-attempt the challenges just to ensure my problem-solving skills remain in shape.

What is a Palindrome?

A palindrome is a word or sentence that's spelt the same way both forward and backwards, ignoring punctuation, case, and spacing.

This challenge is a very simple one but you might be frustrated when your code failed to pass some tests. If you are to be working on alphanumeric characters alone, checking if they are palindrome would have been very easy. Consider a string like this 2_A3*3#A2. How would you turn it into palindrome?

My approach to solving the problem

As you can see from above, some parts of the definition text were emphasized: "Ignoring punctuation, case and spacing". And that's exactly the way to solve the problem. Without delay, let's get started:

I would be showing you how to solve the problem in four basic steps:

  • Step 1: Remove all non-alphanumeric characters: Given the code below:

     function Palindrome (str) {
        // Remove all non-alphanumeric characters in the string
        let convertedStr = str.replace(/[^0-9a-z]/gi, '');
     }
    

    The code above removes all non-alphanumeric characters from the string.

  • Step 2: Convert string to lowercase:

    function Palindrome (str) {
      // Remove all non-alphanumeric characters in the string
      let convertedStr = str.replace(/[^0-9a-z]/gi, '');
      //Step 2: convert string to lowercase
      let lowerCaseStr = convertedStr.toLowerCase(); 
    }

With the code above, we converted our new string to lowercase.

  • Step 3: Revert string:

     function Palindrome (str) {
        // Remove all non-alphanumeric characters in the string
        let convertedStr = str.replace(/[^0-9a-z]/gi, '');
        //Step 2: convert string to lowercase
        let lowerCaseStr = convertedStr.toLowerCase(); 
        //Step 3: Revert string
        let revertedStr  = lowerCaseStr.split("").reverse().join("");
      }
    

    With the code above, we reverted our new lowercase string.

  • Step 4: Check for Palindrome

    function palindrome(str) {
      //Step 1: remove all alphanumeric character
      let convertedStr = str.replace(/[^0-9a-z]/gi, '');
      //Step 2: convert string to lowercase
      let lowerCaseStr = convertedStr.toLowerCase(); 
      //Step 3: Revert string
      let revertedStr  = lowerCaseStr.split("").reverse().join("");
      //Step 4: Check for palindrome
      if(revertedStr === lowerCaseStr) return true;
      return false;
    }
    

    With the code above, we checked if the given string is a palindrome by comparing its lowercase to the reverted format. If it is a palindrome, it will return true else, it will it return a false.

Thank you guys for following through with the tutorial till the end. Feel free to drop your solutions and comments in the comment section below. Do have a great week.