Check for Palindromes weird

Check for Palindromes weird
0.0 0

#1

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++ ){
    if(split[i]!==reverse[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:


#2

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


#3

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.


#4

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?


#5

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

split.reverse();

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.


#6

Oh, right. I forgot about that. Thanks!