This is a classic question, and there are multiple ways to solve this. For the sake of learning, let's cover all of them!
Using built-in methods

This would probably be invalid in an actual interview, but you can rely on the built-in String
method to accomplish a quick reversal. In Javascript, you can simply call reverse()
and in Python, you can call [::-1]
You can then compare the reversed string to the original:
xxxxxxxxxx
12
function isPalindrome(str) {
// Calling reverse function
const reversed = str.split('').reverse().join('');
​
// Checking if both strings are equal or not
if (str == reversed) {
return true;
}
return false;
}
​
console.log(isPalindrome('racecar'));
OUTPUT
:001 > Cmd/Ctrl-Enter to run, Cmd/Ctrl-/ to comment