DEV Community

builtbyjosh
builtbyjosh

Posted on

JS DS&A Palindrome

A very common JavaScript algorithm you may come across is to check if a string is a palindrome. This means that the word is spelled the same forwards and backwards. Take the word 'racecar' for example. If you reverse the string, it will still say 'racecar' and is therefore considered a palindrome.

This blog post will cover two different methods for checking if a string is a palindrome. The first method will utilize built-in functions. The second method will use a for loop.

So we will start with an empty function that has a 'str' passed to it that will represent the word we are checking.

const isPalindrome = (str) => {

}
Enter fullscreen mode Exit fullscreen mode

The First Method:

  1. We will begin by converting the string into an array by using the split() method to break apart the word into individual letters. Then save that to a variable called strArr.
let strArr = str.split('')
Enter fullscreen mode Exit fullscreen mode
  1. Next we will use the reverse() method to reverse the array of letters. And this will be saved to a variable called revArr
let revArr = strArr.reverse()
Enter fullscreen mode Exit fullscreen mode
  1. With the array of letters reversed, we now have to join it all back together using the join() method and save that as revStr.
let revStr = revArr.join('')
Enter fullscreen mode Exit fullscreen mode
  1. Once that is complete, it is time to check if revStr and str are the same and return true or false.
return (str === revStr)
Enter fullscreen mode Exit fullscreen mode

The final function will look like this:

const isPalindrome = (str) => {
    let strArr = str.split('')
    let revArr = strArr.reverse()
    let revStr = revArr.join('')
    return (str === revStr)
}
Enter fullscreen mode Exit fullscreen mode

JavaScript also allows you to chain together methods that can reduce the amount of code you have to write. So we can shorten this to:

const isPalindrome = (str) => {
    let revStr = str.split('').reverse().join('')
    return (str === revStr)
}
Enter fullscreen mode Exit fullscreen mode

The Second Method:

  1. This will start off with the same empty function as before with a 'str' passed into it. But we will start by declaring two variables, and empty string and empty array.
let arr = []
let revStr = ''
Enter fullscreen mode Exit fullscreen mode
  1. Now we will use a for in loop to extract each letter from the 'str' and push that letter to the empty array.
for (letter in str) {
    arr.push(str[letter])
}
Enter fullscreen mode Exit fullscreen mode
  1. Next we will use the same loop, but instead of pushing each letter to an array, we will use 'str' as a counter. We will then pop off the last element of the array and save it to 'revStr'.
for (letter in str) {
    revStr += arr.pop()
}
Enter fullscreen mode Exit fullscreen mode
  1. Now we can just check equality between the original 'str' and 'revStr'
return (str === revStr)
Enter fullscreen mode Exit fullscreen mode

All together, the function would look like:

const isPalindrome = (str) => {
    let arr = []
    let revStr = ''
    for (letter in str){
        arr.push(str[letter])
    }
    for (letter in str){
        revStr += arr.pop()
    }
   return (str === revStr)
}
Enter fullscreen mode Exit fullscreen mode

One problem you might run into with this is if the word is capitalized. So while racecar === racecar, Racecar != racecaR.

The easiest way to fix this issue is to manipulate the 'str' so that all letters are lower case using JavaScripts toLowerCase() method.

Within the function you would need create a variable called 'lowerStr' and save the 'str' with all lower case letters to it.

let lowerStr = str.toLowerCase()
Enter fullscreen mode Exit fullscreen mode

Then anywhere in the function where you are calling 'str', replace it with 'lowerStr' and that should take care of capitolization.

Top comments (0)