Javascript Array Prototype none()

Introduction

none() function returns true only if the predicate supplied returns false for all the items in the array

[-1, 2, 3].none(isLessThanZero) => false
[-1, -2, -3].none(isGreaterThanZero) => true

Array.prototype.none = function (p) {
  for(let i = 0, j = this.length; i < j; i++) {
    if(p(this[i])) return false;
  }/* w  w  w. j a v  a 2 s.com*/

  return true;
};
function isGreaterThanZero(num) {
  return num > 0;
}

function isLessThanZero(num) {
  return num < 0;
}

console.log([-1, 2, 3].none(isLessThanZero));
console.log([-1, -2, -3].none(isGreaterThanZero));



PreviousNext

Related