Instructions
Type the JavaScript code exactly as shown, including indentation, spaces, brackets, parentheses, and semicolons. Focus on accuracy first before improving speed. This test is based on a real data structures and algorithms example, so every character matters.
javascript-binary-search-typing-test
Monokai
function binarySearch(arr, target) {↵
let left = 0;↵
let right = arr.length - 1;↵
↵
while (left <= right) {↵
const mid = Math.floor((left + right) / 2);↵
↵
if (arr[mid] === target) {↵
return mid;↵
}↵
↵
if (arr[mid] < target) {↵
left = mid + 1;↵
} else {↵
right = mid - 1;↵
}↵
}↵
↵
return -1;↵
}↵
↵
const numbers = [1, 3, 5, 7, 9, 11, 13, 15];↵
console.log(binarySearch(numbers, 9));
function binarySearch(arr, target) {↵
let left = 0;↵
let right = arr.length - 1;↵
↵
while (left <= right) {↵
const mid = Math.floor((left + right) / 2);↵
↵
if (arr[mid] === target) {↵
return mid;↵
}↵
↵
if (arr[mid] < target) {↵
left = mid + 1;↵
} else {↵
right = mid - 1;↵
}↵
}↵
↵
return -1;↵
}↵
↵
const numbers = [1, 3, 5, 7, 9, 11, 13, 15];↵
console.log(binarySearch(numbers, 9));