With JavaScript you’re definitely going to come across the palindrome question at some point. First, what is a palindrome? For those of you who haven’t encountered this yet, a palindrome is a word, sentence, number, etc. that reads the same backward as forward. So in a bit more technical speak let’s say- a string that remains the same when it’s rearranged in reverse.
Checking for a palindrome seems to be a popular technical interview question. There are multiple ways to solve this, and it seems the opinions are quite strong on what’s best course of action. I’m not going to into what’s considered best or even most efficient, just illustrate a few ways that this question can be answered.
I was recently asked this in a technical interview. I decided to go a little more in depth, my thoughts being that this way I could showcase my process of breaking down the problem into steps and explain how those steps would get me to the desired outcome. As an aside, my solution did have the correct output, so I did something right. However, when discussing my chosen solution at the end I was encouraged to in the future do it with less code (i.e. using built-in methods).
Please keep in mind in this particular setting edge cases were ignored. The instructions were simple enough-write a function that checks if the given input is a palindrome.
Here is how I decided to originally solve this:
function palindrome(str){
let reverse = '';
// the reverse is an empty string for now
// loop through the string starting from the end to get each character
for (let i = str.length - 1; i >= 0; i--){
// add each character to the reverse
reverse += str.charAt(i);
}
// if true (str is stricly equal to reverse)
return str === reverse;
}
console.log(palindrome('racecar')) // true
console.log(palindrome('palindrome')) // false
If I had solved this with the built-in methods it would look like this:
function palindrome(str){
return str === str.split('').reverse('').join('')
}
console.log(palindrome('racecar')) // true
console.log(palindrome('palindrome')) // false
Short, sweet and right to the point.
Here is another breakdown of how this could be solved:
function palindrome(str){
let reverse = '';
for (let i = 0; i < str.length/2; i++){
if (str[i] !== str[(str.length - 1) - i]){
return false;
}
}
return true;
}
console.log(palindrome('racecar')) // true
console.log(palindrome('palindrome')) // false
And finally, I just saw this solution fairly recently- starting at two separate points, the beginning of the input and end of the input and checking if the two points match or not (forgive me for not sharing the source, I can’t recall where I saw this solution):
function palindrome(str) {
let beginning = 0;
let end = str.length - 1;
while (beginning <= end) {
// if the beginning character is not equivalent to the end character, it's not a palindrome
if (str[beginning] !== str[end]) return false;
// increment the beginning, decrement the end until you meet in the middle
beginning++;
end--;
}
return true;
}
console.log(palindrome('racecar')) // true
console.log(palindrome('palindrome')) // false
There you have it, a few ways to check if something is a palindrome in JavaScript. If you’ve got another way of solving this, please feel free to share!
Top comments (0)