negate boolean function in javascript -
how negate boolean function?
such using like:
_.filter = function(collection, test) { var tmp = [] _.each(collection, function(value){ if (test(value)) { tmp.push(value); } }) return tmp }; var bag = [1,2,3]; var evens = function(v) { return v % 2 === 0};
this wrong:
// returns opposite of evens var result = _.filter(bag, !evens);
result:
[1,3]
try making function returns function:
function negate(other) { return function(v) {return !other(v)}; };
used this:
var result = _.filter(bag, negate(evens));
or declare function when call it:
var result = _.filter(bag, function(v) {return evens(v)});