Write a Palindrome Check function in JavaScript using string and array methods

Share this video with your friends

Social Share Links

Send Tweet
Published 4 years ago
Updated 3 years ago

In this lesson you will learn how to answer a typical beginner interview question of writing a function that check for a string to be a palindrome(reads the same backward as forward).

We will employ string methods like split() and toLowerCase() as well as array methods like reverse() and join(). After writing a step by step function to perform the check we refactor it to be shorter but still readable and working.

Dimitri Ivashchuk: [0:00] Imagine you were asked at the interview to write the palindrome check function. Let's first understand what is palindrome. Palindrome is a word which can be read from both sides the same way, so rotor from left to right reads rotor, rotor from right to left reads the same.

[0:18] We want to write the function that would check if string is a palindrome. Let's start, const palindromeCheck. We want to grab a string and then we want to write some operations, and then return true or false. If the string is palindrome we'll return true, if the string is not palindrome then we'll return false.

[0:45] Let's first understand what is the steps that we need to do to write the function. We need to grab this string that is passed to a function and we need to rotate it. Just for the sake of showing, notPalindrome would be 'teststring'. This obviously doesn't read from both sides the same way.

[1:18] We need to write some code to reverse it first, and then compare it with a string that we pass. In case of this one, we want to receive something like 'gnirtstset'.

[1:36] First, we need to split the string, so reversedString = string.split. Then we want to split it just every letter. Then we want to reverse an array. Then we want to join by every letter. Let's console.log(reversedString) for now. Let's do the palindromeCheck and pass first the notPalindrome.

[2:25] You see that that we got the string that I was talking about. It's basically the teststring reversed. If we pass that string, which is a palindrome, I see that it's basically the same. To finish off our function, we need to compare. In return statement, let's just do if the string === reversedString. Let's console.log(palindromeCheck). You see that it's true, notPalindrome, false. It's working fine.

[3:14] Although this function is already something that is working, there is a common mistake here that beginners usually make when writing the palindromeCheck function, so it doesn't check for every edge case. Imagine that we have the Rotor capitalized. Then if we pass our string here, we get false, although technically it's a palindrome.

[3:39] What we also need to check for is we first need to lowercase the teststring. Let's see how we do that. Const lowerCaseString = string.toLowerCase(). What we want to do now, we want to do the reverse our lowerCaseString and compare the lowerCaseString with our reversedString.

[4:13] We see immediately that the function now returns true, which means whatever we add in terms of the letters, it will still work. Here we can also do whatever we want, and it doesn't break the functionality.

[4:35] For the purposes of this tutorial, I've separated all the operations into separate lines and I've used the explicit return where we check the equality of the strings, but it actually can be written in a really simplified way with the implicit return with the arrow function. It would look something like this.

[4:59] Again, we have our string that we pass, lowercase it and compare it to the string that we...Again, lowercase it, split, reverse the array that we got from the split, and then join the old letters from the array and get in the reversedString. Let's see if this one is working also. We save it, pass our teststring to it, and see that the returned value is true.

[5:29] The function is still working, but it's written in a concise way.

Piyush Chatterjee
Piyush Chatterjee
~ 3 years ago

sound is very low..

Markdown supported.
Become a member to join the discussionEnroll Today