samedi 28 novembre 2015

Filtering out specific values from an array

destroyer([1, 2, 3, 1, 2, 3], 2, 3) should return [1, 1], but it returns [1, 2, 3, 1, 2, 3]. What is wrong with this code?

function destroyer(arr) {
  // Remove all the values
  var arg_num = arguments.length;

  var new_arr = arguments[0].filter(function(a){
    for (var i = 1; i < arg_num; i++){
      if (a === arguments[i]) a = false; 
    }
    return a;
  });

  return new_arr;
}

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




Aucun commentaire:

Enregistrer un commentaire