Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const doSearch = function (array, targetValue) {
- let minIndex = 0;
- let maxIndex = array.length - 1;
- let guessIndex;
- console.log(minIndex,maxIndex,guessIndex)
- while(maxIndex >= minIndex){
- guessIndex = Math.floor((maxIndex+minIndex)/2)
- console.log({minIndex, max, guessIndex});
- console.log('==========');
- console.log(array[guessIndex], targetValue);
- console.log("==========");
- if(array[guessIndex] === targetValue) {
- return guessIndex
- } else if ( array[guessIndex] < targetValue ) {
- minIndex = guessIndex + 1;
- } else {
- max = guessIndex -1
- }
- }
- return -1 // value not present
- }
- const array = [ 0, 8, 16, 20, 45, 65, 89, 100]
- console.log(doSearch(array, 89))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement