Check for Palindromes - Can anyone tell me where i am going wrong?

Here is what I have got so far but I am unsure why my code does not complete the test.

Your code so far

function palindrome(str) {
  // remove non-alphanumerical characters and transform to lower case
  var strClean = str.replace(/[0-9a-z]/gi, '').toLowerCase();

  // convert string to an array
  var strArray = str.split('');

// reverse the array
  var strArrayReverse = strArray.reverse();

// compare each element of array and reverse array
  for(var i = 0; i < strArray.length; i++){
    if(strArray[i] === strArrayReverse[i]){
      return true;
    } else {
      return false;
    }
  }

}



palindrome("eye");

Your browser information:

Your Browser User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36 Edge/15.15063.

Link to the challenge:

  1. You are not using the strClean variable
  2. When you use the method reverse() it saves the reverse array in the same object, so strArray is reversing too.
  3. An array can’t be copied using the = operator. They are referenced, it means that when you change the original “the copy” is also changed

A solution for this is, for example change the strArrayReverse line making it a “split” of the original str and then reversing the same.

You also need to return true only

if all the letters are the same forward and backward. It means after check them all

that’s a clue

~Greetings, and good luck.

Thanks for the help @JScars and @camperextraordinaire I ended up with this solution after fixing my mistakes

function Palidrome(str){
    // remove all non-alphanumerical characters
    var newStr = str.replace(/[^0-9a-z]/gi, '').toLowerCase();
    // convert str to array, reverse the array then join the array back together creating a new string 
    var reverseStr = newStr.split("").reverse().join('');
    // compare the cleaned string and the reversed string
   if(newStr === reverseStr){
       console.log("true");
   } else {
       console.log("false");
   }
}

if(newStr === reverseStr){
return true;
} else {
return false;
}

I had written my code using VScode so I needed to print the result to the console.

What a smart solution :hushed:!
the split doesn’t changes the original array so the newStr variable keep original and you generates a copy.
then you reverse the copy array.
and finaly you joint the copy array.

Excelent! a big demostration of the importance of know the difference beetween value and reference

Thanks for the advice :ok_hand:t5: