Palindrome Checker, why am I wrong? (Solved)

So I tried to do a very simple code to check if a word is a palindrome and to my surprised it worked!
Well half way. For some reason it won’t ever return false. I can’t find logically where it it’s wrong. I could use another set of eyes on it. Thank you!

Your code so far



`function palindrome(str) {
  var newArray = str.split('')
  var newArray2 = newArray.reverse();
if (newArray == newArray2)
  // Good luck!
  return true; 
  if (newArray !== newArray2())
  return false;
}


palindrome("eye");`


**Your browser information:**

User Agent is: `Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.132 Safari/537.36 OPR/63.0.3368.71`.

**Link to the challenge:**
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/javascript-algorithms-and-data-structures-projects/palindrome-checker

image

This is the FIRST thing to consider… the reverse method is not pure, hence it mutates the original array and returns a reference to it.

Also, if it fails after fixing this, it means you haven’t cleaned the string in order to account for case sensitivity, white-space and other stuff.

2 Likes

Hello!

Here’s some info on the .reverse() method: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reverse

You’ll see that it modifies the array in place. So it reverses your newArray as well.

When you set newArray2 to equal newArray, what you are doing is making newArray2 point to the same object as newArray, thus they are always equal.

What you want to do is to create a copy of newArray instead, reverse the copy, and compare the copy to the original newArray. Hope that helps!

Edit: I would also look at your second if statement; the parenthesis shouldn’t be at the end of newArray2

1 Like

Thanks for the help you guys.