Javascript Array search(value)

Description

Javascript Array search(value)

Array.prototype.search = function(value){
 // var this = [];
  //this = this.toTwenty();
  var startIndex  = 0,
  stopIndex = this.length - 1,//from  w ww .  j  av  a2s  .  c o m
  middle = Math.floor((stopIndex + startIndex)/2);
  var count = 0;
  while(this[middle] != value && startIndex < stopIndex){
    count+=1;
    if (value < this[middle]){
      stopIndex = middle - 1;
    }else if (value > this[middle]){
      startIndex = middle + 1;
    }
    middle = Math.floor((stopIndex + startIndex)/2);
  } 
  var l = 0;
  l = this.length;
  var index = (this[middle] != value) ? -1 : middle;
  return ['index : ' + index, "count : " + count, "Length : "+ l]
}

module.exports = Array.prototype;



PreviousNext

Related