I am coding my own function for a binary search algorithm and I can't seem to find the discrepancies in logic. When ever I search for 4 it does not return the ideal response.
Code Below:
var list = [1,2,3,4,6,7,13,18,19];
function binarySearch(list,number) {
var newList = list;
while (newList.length >= 1) {
var halfNum = Math.round(newList.length/2);
if (newList[halfNum] === number) {
return "Number Found";
} else if (newList[halfNum] < number) {
newList = newList.slice(halfNum + 1,newList.length - 1);
} else {
newList = newList.slice(0,halfNum - 1);
}
}
}
console.log(binarySearch(list,4));