Skip to content

Instantly share code, notes, and snippets.

@AntonisFK
Created April 7, 2016 00:53
Show Gist options
  • Save AntonisFK/1df0cf8d983f83f16c4b1129247797a2 to your computer and use it in GitHub Desktop.
Save AntonisFK/1df0cf8d983f83f16c4b1129247797a2 to your computer and use it in GitHub Desktop.
iBS takes in an array and a value to search for. If the value is found in the array then iBS will return the index where the found value is. If the value is not found in the array then iBS returns false. Uses max min and mid
var iBs = function(arr, num){
var max = arr.length-1, min = 0; midpt = Math.floor(arr.length/2);
for(var i=0; i<arr.length-1; i++){
if(arr[midpt]> num){
max = midpt;
midpt --;
}
else if(arr[midpt]<num){
min = midpt;
midpt ++;
}
}
if(arr[midpt] === num){
return midpt;
}else if(midpt === min || midpt === max){
return false ;
}
};
// var arr = [-90,-19,0,2,12,29,33,190,320];
// iBs(arr, 5) => false
// iBs(arr, 12) => 4
// iBs(arr, 0) => 2
// iBs(arr, 190) => 7
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment