Check for Palindromes weird

Tell us what’s happening:

Your code so far

function palindrome(str) {
  // Good luck!
  var split = str.split("");
  var reverse = split.reverse();
  for(var i= 0; i<split.length; i++ ){
      return false;
    } else {return true;}
  return split;

palindrome("not a palindrome");

Your browser information:

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

Link to the challenge:

I’m not concerned with the for loop as so much as the variables. When I return split variable, it’s reversed.

This is weird indeed. I would imagine it may have something to do with references or something else that I lack knowledge in. If you want to fix it, just make the reversed variable be var reverse = str.split(").reverse()'. Also even if you do this, your code will not solve the code. Just remember that punctuation is excluded from the palindrome so keep that in mind.

You did not include an answer to “Tell us what’s happening”. What have you tried? What isn’t working as expected? What tests are failing? What don’t you understand? What do you understand?

The reverse method reverses an array in place, so when you write:


the split array gets reversed. So when you write:

var reverse = split.reverse();

split gets reversed and then a reference to split gets assigned to the variable reverse. After the above line executes, both split and reversed refer to the same array, so any changes you make the elements of one array is reflected in the other.

Oh, right. I forgot about that. Thanks!