Javascript Array indexOfGreatestLessThan(val)

Description

Javascript Array indexOfGreatestLessThan(val)

Array.prototype.indexOfGreatestLessThan = function(val) {
 var minIndex = 0; 
 var minDist = Number.MAX_VALUE;
 for (var i = 0; i < this.length; i++) {
  if ((this[i] <= val) && (Math.abs(this[i] - val) < minDist)) {
   minDist = Math.abs(this[i] - val);
   minIndex = i;//from w w  w  .j a va  2  s  . c  om
  }
 }
 return minIndex;
}



PreviousNext

Related