Code template fill-in

Build JavaScript template fluency.

Fill in the missing pieces of common interview templates. Answers are normalized for whitespace so the drill stays focused on the important code.

Binary Search

Binary Search

1 / 20
function binarySearch(nums, target) {
  let left = 0;
  let right = nums.length - 1;

  while ( <= ) {
    const mid = Math.floor((left + right) / 2);

    if (nums[mid] === target) return mid;

    if (nums[mid] < target) {
       = mid + 1;
    } else {
       = mid - 1;
    }
  }

  return -1;
}