intermediate
Binary search boundaries
Complete the classic binary search — the bounds, the loop condition, and the midpoint that everything else leans on.
/**
* Returns an index i with sorted[i] === target, or -1 when the target is
* absent. `sorted` is ascending and may contain duplicates.
*/
export function binarySearch(sorted: number[], target: number): number {
let low = 0
let ____ = sorted.length - 1
while (low ___ high) {
const ___ = Math.floor((low + high) / 2)
const guess = sorted[mid]
if (guess === target) return mid
if (guess < target) {
low = mid + 1
} else {
high = mid - 1
}
}
return -1
}0/3 filled tab moves · Ctrl+↵ submits · Ctrl+Shift+↵ runsstarter code