Create a function which returns true if the parameter is a palindrome.
A string which is the same forwards as it is backwards.
// Solution//from w w w .j a v a 2s .c om function palindrome(str) { var reverse = str.replace(/[\s]/g, "").split('').reverse().join(''); str = str.replace(/[\s]/g, ""); return (str === reverse ? true : false); } // Output console.log(palindrome("racecar")); // => true console.log(palindrome("never odd or even")); // => true console.log(palindrome("eye spy")); // => false
.replace(/[\s]/g, "") - Replace all whitespace with an empty string.