Exercise 2: Paldinromes

Directions

Given a string, return true if the string is a palindrome or false if it is not. Palindromes are strings that form the same word if it is reversed. Do include spaces and punctuation in determining if the string is a palindrome.

Examples:

palindrome("abba") === true
  palindrome("abcdefg") === false

Solution

In [3]:
function palindrome(str) {
  return str === str.split("").reverse().join("");
}

Alternative Solution

Double comparison issue: compare last to first and first to last

In [2]:
function palindrome(str) {
  return str.split('').every((char, idx) => char === str[str.length -1 - idx]); // length is avail for arr too
}
In [4]:
palindrome("abba")
Out[4]:
true
In [5]:
palindrome("abcdefg")
Out[5]:
false