Check for palindromes - need help

Check for palindromes - need help
0

#1

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”);


#2

Consider:

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

#3

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


#4

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


#5

(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.