Implementing a Palindrome Checker in JavaScript

Sharing is Caring

A palindrome is a word or sentence that’s spelled the same way both forward and backwards. Most of the time when doing these challenges during programming interviews you’re told to ignore punctuation, case and spacing.

I usually start by declaring a function that takes in the phrase and returns a boolean value (true / false) depending on whether it is or not.

'use strict'
function isPalindrome(theString) {
return true
}

I like to start by stripping out everything that isn’t an ascii letter and then you need to reverse a copy of the string and see if they’re the same. The easiest way to do this is to use a regex like this

'use strict'

function isPalindrome (stringToCompare) {
  if (stringToCompare) {
    const cleanedString = stringToCompare.replace(/[^a-z]/gi, '')
  }

  return true
}

Now , this isn’t quite correct but it’s a good start. Now we need to lowercase the stringToCompare. We can easily use the .toLowerCase() method right before we do the replace.

'use strict'

function isPalindrome (stringToCompare) {
  if (stringToCompare) {
    const cleanedString = stringToCompare.toLowerCase().replace(/[^a-z]/gi, '')
  }

  return false
}

Now depending on the test, we might not be able to use built in array methods or we might be able to! Let’s assume for now that we can, we can split the string into an array of characters. To do this, we’ll use the split method!

function isPalindrome (stringToCompare) {
  if (stringToCompare) {
    const cleanedString = stringToCompare.toLowerCase().replace(/[^a-z]/gi, '')
    const reversedString = cleanedString.toLowerCase().split('')
  }

  return true
}

And then we can call the built in reverse method on the array and convert it back into a string. Then it’s an easy string equivalency check.

'use strict'

function isPalindrome (stringToCompare) {
  if (stringToCompare) {
    const cleanedString = stringToCompare.toLowerCase().replace(/[^a-z]/gi, '')
    const reversedString = cleanedString.toLowerCase().split('').reverse().join('')
    return cleanedString === reversedString
  }

  return false
}

We can test our method by calling in various palindromes and strings that aren’t.

'use strict'

function isPalindrome (stringToCompare) {
  if (stringToCompare) {
    const cleanedString = stringToCompare.toLowerCase().replace(/[^a-z]/gi, '')
    const reversedString = cleanedString.toLowerCase().split('').reverse().join('')
    return cleanedString === reversedString
  }

  return false
}

// Should Return true!
console.log(`raceCar = ${isPalindrome('raceCar')}`)

// Should Return true!
console.log(`Never odd or even = ${isPalindrome('Never odd or even')}`)

// Should Return false!
console.log(`Not = ${isPalindrome('Not')}`)

If you aren’t allowed to use built in methods you’ll need to use some loops and basically take the same approach. Start with a simple function and clean out all of the non letter characters.

I’ve never had anyone complain about my regex for removing non alpha characters so I feel comfortable using that approach right at the start and then building an array of the individual characters.

'use strict'

function isPalindrome (stringToCompare) {
  if (stringToCompare) {
    const cleanedString = stringToCompare.toLowerCase().replace(/[^a-z]/gi, '')

    return true
  }

  return false
}

// Should Return true!
console.log(`raceCar = ${isPalindrome('raceCar')}`)

// Should Return true!
console.log(`Never odd or even = ${isPalindrome('Never odd or even')}`)

// Should Return false!
console.log(`Not = ${isPalindrome('Not')}`)

We then have to build an array of the individual characters in reverse. This is a pretty easy problem to solve when you remember that you can use a for loop or a while loop and just decrement it.

'use strict'

function isPalindrome (stringToCompare) {
  if (stringToCompare) {
    const cleanedString = stringToCompare.toLowerCase().replace(/[^a-z]/gi, '')
    const reversedString = getReversedString(cleanedString)

    return cleanedString === reversedString
  }

  return false
}

function getReversedString (stringToReverse) {
  let reversedString = []

  for (let i = stringToReverse.length; i > -1; i--) {
    reversedString.push(stringToReverse[i])
  }

  return reversedString.join('')
}

// Should Return true!
console.log(`raceCar = ${isPalindrome('raceCar')}`)

// Should Return true!
console.log(`Never odd or even = ${isPalindrome('Never odd or even')}`)

// Should Return false!
console.log(`Not = ${isPalindrome('Not')}`)
Sharing is Caring