Javascript Array findIf(predicate)

Description

Javascript Array findIf(predicate)

/// Find value by predicate, return found position or -1 if not found
Array.prototype.findIf = function(predicate) {
 for (var i = 0, len = this.length; i < len; ++i) {
  if (predicate(this[i])) {
   return i;/*from   w  w w  .j  av a  2  s  .  c  o  m*/
  }
 }
 return -1;
}

function rand(min, max) {
 return Math.floor(Math.random() * (max - min + 1)) + min;
}



PreviousNext

Related