Palindrome Checker - loop won't work wondering why

Tell us what’s happening:

Your code so far


function palindrome(str) {
  // Good luck!
  //split str into char array
  var strArr = str.split('');
  console.log('strArr: ' + strArr);

  //reverse char array
  var strArrRev = strArr.reverse();
  console.log('strArrRev: ' + strArrRev);

  //check if both arrays are the same
  for (var i = 0; i < strArr.length; i++) {
    console.log('Im in a loop');
    if ((strArr[i]) !== (strArrRev[i])) {
      console.log('strArr i: ' + strArr[i]) ;
      console.log('strArrRev i: ' + strArrRev[i]);

      console.log(strArr[i] + ' ' + strArrRev[i]);
      console.log(false);
      return false;
    }else {
      console.log('Im in else statement') ;
      console.log(true);
      return true;
    }
  }
}

palindrome('tye');

Your browser information:

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

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

First remove the else statement from the loop. this is causing only the first letter to be checked and if it is the same then it returns true and stops the function. Make sure when you are debugging loops you run through their logic path.

Second, your arrays are technically the same array. The comparison in the loop is checking if the objects are the same, and they are. To avoid this you can use the spread operator ([…]) like below.

let str = "pasta";
let strArr = str.split("");
let strArrRev = [...strArr].reverse();

This will make a copy of the contents of the variable thus creating a distinct object.

At some point you will probably have to use regExp to remove unwanted symbols and spaces for this project, and when doing that a loop is not required and is less efficient.

2 Likes

Well, @DaxHarley, your first point is right. The return in both branches of the if statement causes the function to return in every case after checking the first letter. The idea would be sound, as it should break at the first instance of not matching, but as you say, the else branch breaks things.

Your second point, though, is unclear. inline edit, I’m an idiot. I was going to say that Array.reverse() returns a new Array which is being assigned to an empty variable. However, by reading about Array.prototype.reverse(), I find that in fact strArr.reverse() does what’s known as “reverse in place” – which means that strArr is being reversed in strArr itself, then THAT value is being assigned to strArrRev. You are, again, absolutely right. Using something like the spread operator creates a clone of the array, then reverses THAT. Great catch.

And yes, your third point is accurate. The OP will need to remove any non-alphanumeric from the string, and regexp is one of the more painless ways…

1 Like

Great!
Thanks so much for the help, will refactor that right away.
:tired_face: