A palindrome is a word, phrase, number, or other sequence of symbols or elements, whose meaning may be interpreted the same way in either forward or reverse direction. Famous examples include "Amore, Roma", "A man, a plan, a canal: Panama" and "No 'x' in 'Nixon'". - wikipedia
Our goal is to determine whether or not a given string is a valid palindrome or not.
Test cases:
Test.assertEquals(palindrome("Amore, Roma"), true)
Test.assertEquals(palindrome("A man, a plan, a canal: Panama"), true)
Test.assertEquals(palindrome("No 'x' in 'Nixon'"), true)
Test.assertEquals(palindrome("Abba Zabba, you're my only friend"), false)
My code so far:
function palindrome(string) {
var str = string.toLowerCase().replace(/[^a-z]+/g,"");
var rev= str.split("").reverse().join("");
if (string == rev) {
return true;
} else {
return false;
}
}
Apparently join
is undefined but I don't understand why?
I tried your examples with the following changes and it works on OSX 10.9:
function palindrome(string) {
var str = string.toLowerCase().replace(/[^a-z]/g, "");
var rev = str.split("").reverse().join("");
return (str == rev);
}
It appears the array join() method has been part of Javascript since version 1.1 -- both the specific error message and some description of your environment should help resolve this.