Comparing two arrays for palindrome

Tell us what’s happening:
I already created two arrays to compare values. The first one removes all “special” characters, makes alphabetic characters into lower case and splits each characters into elements. My second array (revArray) takes the initial string makes alphabetic characters into lower case, splits each character and then reverse its position in the array.

I want to compare this two array with an if function (or any other function). I want to verify if the elements in the arrays are the same even when reversed (palindrome). I tried to create an simple if function using the === operator but that did not work out. I did see some stack overflow post saying I should use the .length method to compare the two arrays but I’m not sure how to apply that in my code or what is the reason behind it. Any help is appreciated, even if it is just link. Thank You

Your code so far

function palindrome(str) {
  // Good luck!
   var newArray = str.replace(/[\W_]/g, "").toLowerCase().split("");
   var revArray = str.toLowerCase().split("").reverse("");
         
    return newArray;
      
}

palindrome("bong");

Your browser information:

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

Link to the challenge:
https://www.freecodecamp.org/challenges/check-for-palindromes

You can’t just compare two arrays, they’re objects and even if the contents of the two arrays are identical, the arrays themselves will not be. Compare strings, not arrays, then you can use === fine, it makes much more sense. Otherwise you’ll have to loop through the arrays and check they each have exactly the same element in the same position.

Yes I have to loop through the arrays and check they each have exactly the same element in the same position. The purpose of the exercise is finding words, phrases or even sentences that when reversed spell the same thing.

i want to use an if statement but Im not sure how to apply one here.

array1.join('') === array2.join('')? Your aim is, in the end, to check if a string is the reverse of another; trying to compare array elements one by one makes your task 100× harder.

Note also you need to do the replace in your revArray else what you have there won’t work.

1 Like

Thank you for your response on this topic, I did what you suggested and it worked partially. Some of the challenges were not been check-marked like “A man, a plan, a canal. Panama”. Then I figured I forgot to include the .toLowerCase lol.