Recently, I attempted to implement binary search in my Chrome console. However, upon running the code, it caused the entire browser to freeze and I had to forcefully close the tabs:
var array = [1, 3, 5, 8];
var performBinarySearch = function (array, target) {
var lowerBound = 0;
var upperBound = array.length - 1;
var middle = Math.floor((upperBound + lowerBound) / 2);
while (lowerBound <= upperBound) {
if (target === array[middle]) {
return middle;
} else if (target > array[middle]) {
lowerBound = middle + 1;
} else {
upperBound = middle - 1;
}
}
return -1;
};
console.log(performBinarySearch(array, 3));