Tuesday, October 20, 2015

Bonfire : Where do I Belong

function where(arr, num) {
  // Find my place in this sorted array.
   arr.sort(function(a, b) {
  return a - b;
});
 
//at this point the array is all sorted
 //the below function gets the maxnumber in the array
  function maxnumberinarray( array ){
    return Math.max.apply( Math, array );
};
  if(num>maxnumberinarray(arr)){
    return arr.length;
  }
  var pos = 0;
  for(i=0;i<arr.length;i++)
    {
      if(arr[i]<num){
        pos = i+1;
      }
      else {
      return pos;
      }
    }
}

where([10, 20, 30, 40, 50], 35)

No comments:

Post a Comment