Advertisement
Guest User

Untitled

a guest
Aug 17th, 2019
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.71 KB | None | 0 0
  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))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement