Чего ему не хватает для прохождения проверки? В замкнутый цикл не впадает, тем не менее пишет про "Exceeded execution timeout of 40 seconds"...
solution.js
import { binarySearch } from './helper.js';
const arr = [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 ];
console.log(binarySearch(arr, 22));
helper.js
export const binarySearch = (arr, x) => {
// implement binary search algorithm here
let lengthIsEven, middleKey, length;
let startKey = 0;
let endKey = arr.length - 1;
while (startKey != endKey) {
length = endKey - startKey + 1;
length % 2 === 0 ? lengthIsEven = true : lengthIsEven = false;
if (lengthIsEven) {
middleKey = length / 2 - 1;
} else {
middleKey = Math.round(startKey + length / 2);
if (arr[middleKey] === x) {
return middleKey;
} else {
--middleKey;
}
}
if (arr[middleKey] === x) {
return middleKey;
} else if (arr[middleKey] > x) {
endKey = middleKey;
} else {
startKey = middleKey + 1;
}
}
return -1;
}