Skip to content

Creating a palindrome checker

Posted on:February 23, 2024 at 06:00 PM

creating-a-palindrome-checker.jpg

Recently, I was asked to create a function to check if a sentence is a palindrome.

And well, I confess that I struggled a little bit to solve the problem. I’ve never really made a function like that and I even didn’t know what was a palindrome.

Then, if you’re wondering in this moment what’s it, here’s a complete description of it.

What’s a palindrome?

A palindrome is a word, phrase, number, or other sequence of characters that reads the same forward and backward, ignoring spaces, punctuation, and capitalization.

In other words, a palindrome retains its meaning even when its characters are reversed.

Some examples of palindromic words are “radar,” “level,” “deified,” and “civic.”

Palindromic phrases include “A man, a plan, a canal, Panama!” and “Madam, in Eden, I’m Adam.”

Palindromic numbers are those that remain the same when their digits are reversed, such as 121, 1331, and 12321.

Palindromes are often used for wordplay, puzzles, and linguistic curiosity.

The palindrome function

After this brief explanation of what’s a palindrome, here’s how the function to check it can be done.

const toLowerCase = (word: string) => word.toLocaleLowerCase();
const removeNonAlphanumerics = (word: string) =>
  word.replace(/[^a-zA-Z0-9]/g, "");
const toArray = (word: string) => word.split("");

export function isPalindrome(word: string): boolean {
  const wordAsArray = toArray(removeNonAlphanumerics(toLowerCase(word)));

  const middleIndex = Math.floor(wordAsArray.length / 2) - 1;

  let start = 0;
  let end = wordAsArray.length - 1;

  while (start <= middleIndex) {
    if (wordAsArray[start] !== wordAsArray[end]) {
      return false;
    }

    start += 1;
    end -= 1;
  }

  return true;
}

You can find the repositories’ code here.

Explaining the code

I don’t think I need to explain every single line of the code above, but it’s important to highlight some important things about.

First of all, you need to transform every character to lowercase.

const toLowerCase = (word: string) => word.toLocaleLowerCase();

You need to remove all non-alphanumeric characters from the string to ensure that all compared characters consist only of letters or numbers.

const removeNonAlphanumerics = (word: string) =>
  word.replace(/[^a-zA-Z0-9]/g, "");

After that you need to convert the string to an array.

const toArray = (word: string) => word.split("");

Yeah, I know that you can just access every string’s character by index directly, but I prefer to convert the string to an array because it’s readable.

Now you can use everything at once.

const wordAsArray = toArray(removeWhitespaces(toLowerCase(word)));

The next step you’ll need to find the middle of the array. That way you can check all the characters by looping the wordAsArray variable.

Let’s start the array index that way:

let start = 0;
let end = wordAsArray.length - 1;

Note that end variable starts at the end of the array.

Ok, now you can just start to check every position of the wordAsArray.

while (start <= middleIndex) {
  if (wordAsArray[start] !== wordAsArray[end]) {
    return false;
  }

  start += 1;
  end -= 1;
}

If a single character from the beginning and end of the wordAsArray be different you can return false instantly. It means that the sentence isn’t a palindrome.

if (wordAsArray[start] !== wordAsArray[end]) {
  return false;
}

If it be equals, you can check the next character.

start += 1;
end -= 1;

When the start variable be greater than middleIndex variable, then the sentence has been all checkedand you can return true because the sentence is a palindrome.