Check for Palindromes: Always returns true

Check for Palindromes: Always returns true
0

#1
function palindrome(str) {
  // Good luck!
  var strArray = [];
  for(var i=0; i<str.length; i++){
    if (str[i].isAlnum){
      strArray.push(str[i]);
    }
  }
  var tempArray = strArray;
  if(tempArray === strArray.reverse()){
    return true;
  }
  
  return false;
}
type or paste code here

this function always return true!!!
what’s the problem??
please help this out!!


I'm struck at 'Check for pallindromes'
#2

There is no such thing as isAlnum in JS. Check out regular expressions if you want to test if a character is alphanumeric.

This doesn’t copy the array in strArray to the tempArray variable. What is assigned here is the reference to that array, meaning you now have two variables that refer to the same array.

Another thing to note is that the reverse function modifies the array in-place. So when you compare tempArray with strArray.reverse(), you’re still comparing the same array with itself.

Even if reverse() returned a new reversed array instead of reversing in-place, you’ll still have a problem with comparing that with tempArray. Even if two arrays have the same contents, the only time when === returns true when comparing arrays is when both sides refer to the same array.

Try converting the arrays first to strings, then you can compare. There’s the .join() function you can use. But until you properly check for the alphanumeric characters, the tests still won’t pass.


#3

thank you so so much my dear friend!!!