SHARE
TWEET

Untitled

a guest Aug 17th, 2019 63 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const doSearch = function (array, targetValue) {
  2.   let minIndex = 0;
  3.   let maxIndex = array.length - 1;
  4.   let guessIndex;
  5.  
  6.   console.log(minIndex,maxIndex,guessIndex)
  7.  
  8.   while(maxIndex >= minIndex){
  9.     guessIndex = Math.floor((maxIndex+minIndex)/2)
  10.     console.log({minIndex, max, guessIndex});
  11.     console.log('==========');
  12.     console.log(array[guessIndex], targetValue);
  13.     console.log("==========");
  14.     if(array[guessIndex] === targetValue) {
  15.       return guessIndex
  16.     } else if ( array[guessIndex] < targetValue ) {
  17.       minIndex = guessIndex + 1;
  18.     } else {
  19.       max = guessIndex -1
  20.     }
  21.   }
  22.  
  23.   return -1 // value not present
  24. }
  25.  
  26. const array = [ 0, 8, 16, 20, 45, 65, 89, 100]
  27.  
  28. console.log(doSearch(array, 89))
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top