binaryserachindex javascript

JavaScript
function binarySearchIndex (array, target, low = 0, high = array.length - 1) {
  if (low > high) {
    return -1
  }
  const midPoint = Math.floor((low + high) / 2)

  if (target < array[midPoint]) {
    return binarySearchIndex(array, target, low, midPoint - 1)
  } else if (target > array[midPoint]) {
    return binarySearchIndex(array, target, midPoint + 1, high)
  } else {
    return midPoint
  }
}let arr = [1, 3, 5, 7, 8, 9];
let binarySearch = (arr, x , strt, end) => {
  if(end < strt) return false;
  let mid = Math.floor((strt + end) / 2);
  if(arr[mid] === x) {
    return true;
  }
  if(arr[mid] < x) {
    return binarySearch(arr, x, mid+1, end);
  }
  if(arr[mid] > x) {
    return binarySearch(arr, x , strt, mid-1);
  }
}

let strt = 0, end = arr.length -1;
let bool = binarySearch(arr, 7, strt, end);
console.log(bool);

Source

Also in JavaScript: