JS Interview Prep: Palindrome

in #javascript7 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