Palindrome help - comparing arrays doesn't work

Palindrome help - comparing arrays doesn't work
0

#1

My process is to take a string, make it lower case, remove any non-characters and then split it up into individual characters in an array, which I then compare the array with a reversed version of the array. For some reason, when the arrays are compared they always return true, even though I pass in text that is not a palindrome. Any help would be appreciated.

function palindrome(str) {
var newStr = str.toLowerCase().replace(/[\W]/g,'').split('');
return newStr === newStr.reverse();
}
palindrome("not a palindrome");

#2

You can not make direct comparisons with arrays with the “===” or the “==” operator. You can use the “===” operator to compare strings and numbers. Why not convert them back to strings first and then make the comparison?

You could use:

.join('')

to join convert an array back into a string of characters (with no spaces in between).