Palindrome algorithm solution with arrays- yay or nay?

Hi, everyone! First time, long time!

Took a lot more time than I’m willing to admit on this particular problem-

Thing is, I’ve noticed that a lot of people aren’t using arrays on their solution- using array methods, yes, but just on strings. When I first read this question, I immediately thought of splitting this into an array so as to pop and shift each match off.

This code passed but I’m wondering what you’d make of this approach.
Thank you for taking the time. :wink:

Your code so far

function palindrome(str) {
  var result = true;

  var stripped = str.replace(/[\W_]/g, '').toLowerCase();  
  var arr = stripped.split('');
  for (var i = 0; i < arr.length; i++){
    if (arr[i] === arr[arr.length-1-i]) {
    } else {
      result = false;
    return result;

palindrome("1 eye for of 1 eye");

Your browser information:

Your Browser User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.140 Safari/537.36.

Link to the challenge:

Not only is push() and pop()ing not helping you, it actually stops your code from working against all cases.
A good rule to stick to is never to modify an array that you are iterating over - especially if you are changing its length. Because you are removing values on every iteration, length changes, so you aren’t actually comparing every letter.
It may happen to work with all the test cases on FCC but look at this:

1 Like

Whoa. Can’t believe that it actually worked. Lookit that.

Thanks for the insight- I’ll rework this and see what I come up with.

Take a pen and paper. Work through what happens right now. (Track each change with that pencil).

What happens if you don’t use push() and pop()?
What happens if you don’t make it an array?