JS Interview Prep: Palindrome

in javascript •  7 years ago  (edited)

Screenshot of Google Chrome (11-23-17, 9-09-30 PM).png

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;
}

References


Every Function

Authors get paid when people like you upvote their post.
If you enjoyed what you read here, create your account today and start earning FREE STEEM!