JS Interview Prep: Palindrome
Question
Given a string, have the function return true or false if palindrome
Examples:
palindrome("abba") === true
palindrome("abcdefg") === false
Answer
Solution 1
function palindrome(str) {
return str.split('').every((c, i) => {
return c === str[str.length - i -1];
});
}
Solution 2
function palindrome(str) {
const reversed = str
.split('')
.reverse()
.join('');
return reversed === str;
}