Nodejs Array Any Predicate any(p)

Here you can find the source of any(p)

Method Source Code

// [-1, 2, 3].any(isGreaterThanZero) => true
// [-1, -2, -3].any(isGreaterThanZero) => false
// *** YOUR CODE HERE ***

Array.prototype.any = function(p) {
    var count = 0
    for (var i = 0; i < this.length; i++) {
        currentIndex = this[i]/*from  w ww  .jav a  2s  .c  o m*/
        if (p(currentIndex) === true) {
            console.log(p(currentIndex), "true is true")
            count += 1
        };
    }
    if (count > 0) {
        return true
    }
    return false
};

// OR

// Array.prototype.any = function (p) {
//   return this.filter(p).length > 0;
// };


// use these to test your array methods!
function isGreaterThanZero(num) {
    return num > 0;
}

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

Related

  1. any()
    Array.prototype.any = function () {
      return this.length > 0;
    
  2. any(conditionFunction)
    Array.prototype.any = function(conditionFunction) {
        try {
            this.each(function(element) {
                if (conditionFunction(element)) {
                    throw 'found!';
            });
            return false;
        } catch(e) {
    ...
    
  3. any(delegate)
    Array.prototype.any = function(delegate) {    
      for (var i=0; i < this.length; i++) {      
        if (delegate.apply(this, [this[i]]) === true) {
          return true;
      return false;  
    };
    
  4. any(func)
    Array.prototype.any = function (func) {
        var length = this.length;
        for (var i = 0; i < length; i++) {
            if (func(this[i]))
                return true;
        return false;
    
  5. any(p)
    Array.prototype.any = function (p) {
        var count = 0
        for (var i = 0 ; i < this.length ; i++) {
          currentIndex = this[i]
          if (p(currentIndex) === true) {
            console.log(p(currentIndex),"true is true")
            count +=1 
          };
        if (count > 0) {
          return true
        return false
    };
    Array.prototype.any = function (p) {
      return this.filter(p).length > 0;
    };
    
  6. any(p)
    Array.prototype.any = function (p) {
      for (var i = 0; i < this.length; i++) {
        if(p(this[i]) == true){
          return true;
      };
      return false;
    };
    
  7. any(p)
    Array.prototype.any = function (p) {
      for(var i = 0; i < this.length; i++){
        if(p(this[i])) return true;
      return false;
    };
    function isGreaterThanZero (num) {
      return num > 0;
    function isLessThanZero (num) {
      return num < 0;
    console.log([-1, 2, 3].any(isGreaterThanZero)); 
    console.log([-1, -2, -3].any(isGreaterThanZero)); 
    
  8. any(p)
    Array.prototype.any = function (p) {
     return this.filter(p).length > 0;
    };
    
  9. any(p)
    Array.prototype.any = function (p) {
      for (i = 0; i < this.length; i++)
        if (p(this[i])) return true;
      return false;
    };