Check for palindromes - need help

Hi. can anybody please tell me whats wrong in this Check for Palindromes code? I think i am doing everything correct but i am getting false for following code :

function palindrome(str) {
// Good luck!
str.toLowerCase();
var newStr = str.replace(/[^a-z0-9]/gi,"");
var array = newStr.split("");
var array2 = array.slice();

array2.reverse();
if(array == array2
) {
return true;
}

else { return false;}
}

palindrome (“race car”);

Consider:

  • Which test is failing?
  • what is different about that string?
  • what exactly is your regex matching?

you are comparing arrays. you should try to compare strings.

http://stackoverflow.com/questions/22395357/how-to-compare-two-arrays-are-equal-using-javascript-or-jquery

1 Like

I was able to get a true result by creating a string from each array with join and comparing the strings.

1 Like

(array == array2) will always be false because array and array2 are different arrays.

The == operator doesn’t compare the values inside the arrays.

It’s easier to compare strings but you can also make your code work by comparing array elements with a loop.

1 Like