binary search code javascript code example
Example: binaryserachindex 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
}
}