JSFiddle - React, Tailwind, and code Playground

by sramnan

JavaScript

function binarySearch(array, target) {
  var min = array[0];
  var max = array.length - 1;
  var guess;
  while (min <= max) {
    guess = Math.floor((min + max / 2));
    console.log(guess);
    if (array[guess] === target) {
      return guess;
    } else if (array[guess] > target) {
      max = guess - 1;
      
    } else if (array[guess] < target) {
      max = guess + 1;
      
    }
    

  }
  return -1;
}

console.log(binarySearch([3,4,5],4));