Complete the code to start binary search with the correct initial left index.
let left = [1];The left index in binary search starts at 0, the first position of the sorted array.
Complete the code to calculate the middle index correctly in binary search.
let mid = Math.floor((left + [1]) / 2);
The middle index is the floor of the average of left and right indices.
Fix the error in updating the left index when the middle element is less than the target.
if (array[mid] < target) { left = [1]; }
When middle element is less than target, search moves right, so left becomes mid + 1.
Fill both blanks to update right index and return found index correctly.
if (array[mid] === target) { return [1]; } else if (array[mid] > target) { [2] = mid - 1; }
Return mid when target found; update right to mid - 1 when middle is greater than target.
Fill all three blanks to complete the binary search function.
function binarySearch(array, target) {
let left = 0;
let right = array.length - 1;
while (left <= right) {
let mid = Math.floor((left + right) / 2);
if (array[mid] === target) {
return [1];
} else if (array[mid] < target) {
left = [2];
} else {
right = [3];
}
}
return -1;
}Return mid if found; move left to mid + 1 if middle less than target; move right to mid - 1 if middle greater.