Search code examples
javascriptarrayspalindrome

Comparing values between two arrays


I'm trying to set up a function that checks if a word or a text is a palindrome. To do that, it splits the text so that every letter is an element of a new array, it takes rid of the white spaces and it makes the reverse array. Then it checks if every element of the two arrays, at the same positions, are equal. If not it returns false, if yes it returns true. Here the function:

function palindrome(str) {
  var low = str.toLowerCase();
  var newArray = low.split("");
  var noSpace = newArray.filter(function(val) {
    return val !== " ";
  });
  var reverse = noSpace.reverse();
  
  function check (a, b) {
    console.log(`checking '${a}' against '${b}'`);
    var partial;
    var result = 1;
    for (var i = 0; i < a.length; i++) {
      console.log(`comparing '${a[i]}' and '${b[i]}'`);
      if (a[i] !== b[i]) {
        result = 0;
      } else {
        partial = 1;
        result *= partial;
      }
    }
    return result;
  }
  
  var result = check(noSpace, reverse);
  if (result == 1) {
    return true;
  } else {
    return false;
  }
  
   
}


palindrome("r y e");

I don't know what's wrong but it seems that the function keeps on returning a true value no matter what word or text I pass to the function. What is wrong with that?


Solution

  • Your issue seems to be because reverse() changes the actual array as well. So doing

    var reverse = noSpace.reverse();
    

    Will reverse noSpace and assign a reference to it on the variable reverse. That is, both arrays will be the same (reversed) array.

    To bypass that, I've used .slice() to create a copy of the original array, and then called .reverse() on that new array, ridding you of any conflicts.

    Here's a working snippet of what it looks like:

    function palindrome(str) {
        var str_array = str.toLowerCase().split("");
        var no_space = str_array.filter(function(val) {
            return val !== " ";
        });
    
        // By applying '.slice()', we create a new array
        // reference which can then be reversed and assigned
        // to the 'reverse' variable
        var reverse = no_space.slice().reverse();
    
        function check(a, b) {
            var partial;
            var result = 1;
            for(var i=0; i < a.length; i++) {
                if(a[i] !== b[i]) {
                    // We don't need to keep
                    // comparing the two, it
                    // already failed
                    return 0;
                } else {
                    // I've kept this part even though
                    // I don't really know what it is
                    // intended for
                    partial = 1;
                    result *= partial;
                }
            }
            return result;
        }
        return check(no_space, reverse) === 1;
    }
    
    console.log(palindrome("a b a"));
    console.log(palindrome("r y e"));