forked from blakeembrey/code-problems
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsorted-array-search.js
More file actions
25 lines (20 loc) · 802 Bytes
/
Copy pathsorted-array-search.js
File metadata and controls
25 lines (20 loc) · 802 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
module.exports = function (array, number) {
return (function searchNumber (left, right) {
var center = left + Math.floor((right - left) / 2);
// If the numbers match, return the center index.
if (array[center] === number) { return center; }
// If the left position is the same as the center position, return -1.
if (left === center) { return -1; }
// When the current number is larger than the search input, recurse
// moving the left and right indexes to the search area.
if (array[center] > number) {
return searchNumber(left, center);
}
// When the current number is smaller move the search parameters to the
// right.
if (array[center] < number) {
return searchNumber(center, right);
}
return -1;
})(0, array.length);
};