Setting low to mid rather than mid plus one stalls, so a guard stops it.
Acceptance criteria
- 1.#out should show index 4.
Setting low to mid rather than mid plus one stalls, so a guard stops it.
function search(items, target) { let low = 0, high = items.length - 1, guard = 0; while (low <= high && guard < 20) { const mid = (low + high) >>> 1; if (items[mid] === target) return mid; if (items[mid] < target) low = mid; else high = mid - 1; guard++; } return -1; } document.querySelector('#out').textContent = "index " + search([1, 3, 5, 7, 9], 9);
Output ready when you are
Press Run to render your code, or Submit to validate your solution.