Seek and Destroy: difference between codes

Tell us what’s happening:

I know that this is a very newbie question but I’ve been struggling to understand the distinction in these two codes.

Your code so far

// This code produces [1,3,1,3]: incorrect
// It only goes through the first test argument.  Why?

function destroyer(arr) {
  var args = Array.prototype.slice.call(arguments);
  var targetArr = args[0];
  var testArr = [];
  for (i = 1; i < args.length; i++) {
    testArr.push(args[i]);
  }

  function isSame(val) {
      for (var j = 0; j < testArr.length; j++){
       // console.log("arr: " + arr);
       // console.log("testArr: " + testArr[j]);
        return val != testArr[j];
      }
    } 
    
  return arr.filter(isSame);
}

destroyer([1, 2, 3, 1, 2, 3], 2, 3);
//This code produces [1,1]: correct

function destroyer(arr) {
    var testArr = [];
    for (var i = 1; i < arguments.length; i++) {
        testArr.push(arguments[i]);
    }

    function isSame(val) {
        for (var j = 0; j < testArr.length; j++) {
            if (val === testArr[j]) {
                return false;
            }
        }
        return true;
    }
    return arr.filter(isSame);
}

(destroyer([1, 2, 3, 1, 2, 3], 2, 3));

Your browser information:

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

Link to the challenge:
https://www.freecodecamp.org/challenges/seek-and-destroy

The first isSame loop always returns a value on the first iteration. In other words, it only ever checks testArr[0]. The isSame in the second solution only returns true if the whole array has been checked.

Interesting. I think I might not be certain how filter() works…

I guess I mean to say that the first code will return true on the first check (i.e. 1 != 2). Why not just exit right there and then?

It will also return false if the first item matches. Only the first item in testArr is ever checked.