I'm trying to implement a binary search algorithm in JavaScript. Things seem okay, but my return statements appear to be returning undefined. Can anybody tell me what's wrong here?
Fiddle: http://jsfiddle.net/2mBdL/
var a = [
1,
2,
4,
6,
1,
100,
0,
10000,
3
];
a.sort(function (a, b) {
return a - b;
});
console.log('a,', a);
function binarySearch(arr, i) {
var mid = Math.floor(arr.length / 2);
console.log(arr[mid], i);
if (arr[mid] === i) {
console.log('match', arr[mid], i);
return arr[mid];
} else if (arr[mid] < i && arr.length > 1) {
console.log('mid lower', arr[mid], i);
binarySearch(arr.splice(mid, Number.MAX_VALUE), i);
} else if (arr[mid] > i && arr.length > 1) {
console.log('mid higher', arr[mid], i);
binarySearch(arr.splice(0, mid), i);
} else {
console.log('not here', i);
return -1;
}
}
var result = binarySearch(a, 100);
console.log(result);
return binarySearch(...)
in each case. – Pushy