Reputation: 391
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");
Upvotes: 0
Views: 1048
Reputation: 104810
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;
}
Upvotes: 2