0

This simple little function is throwing an "unexpected identifier" error and I don't know why.

function palindromeCheck(word) {
    var reversed = word.split("").reverse().join("");
    if (reversed === word) {
    return "Yay, " + word + " is a palindrome!";
  } else {
    return "Drats, " + word + " isn't a palindrome.";
  };
};

palindromeCheck("racecar");
Scott Magdalein
  • 361
  • 1
  • 3
  • 13

1 Answers1

2

You solved your problem, but you may have another-

palindromes can be multiple words (or digits) with punctuation and capital or lower case letters.

'Able was I, ere I saw Elba.' is a palindrome, for example that fails your test.

(So would 'Racecar' or 'race car', which are also palindromes.)

A better test would be to strip everything not a letter or digit, and ignore the case.

function isPalindrome(word){
    var w= word.toLowerCase().replace(/[^0-9a-z]+/g, '');
    for(var i= 0, L= w.length-1;i<L;i++, L--){
        if(w[i]!== w[L]) return false;
    }
    return true;
}
kennebec
  • 102,654
  • 32
  • 106
  • 127