-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpeakFinder.js
More file actions
26 lines (24 loc) · 831 Bytes
/
peakFinder.js
File metadata and controls
26 lines (24 loc) · 831 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
26
const peakFinder = function (nums) {
let testIndex = 0,
compareIndex = nums.length - 1;
// If the length of the array is zero, return 0
if (nums.length < 1) {
return 0;
}
// If length of array is one, return number in array
if (nums.length === 1) {
return nums[0];
}
// If has more than one number, compare the numbers
while (testIndex < compareIndex) {
// Find middle index of array
const midIndex = parseInt((testIndex + compareIndex) / 2);
// If number is greater than one to right
// Then the set the compareIndex to equal the middle index
// Else, set the testIndex to equal middle index plus one or one to the right of the middle
nums[midIndex] > nums[midIndex + 1]
? (compareIndex = midIndex)
: (testIndex = midIndex + 1);
}
return nums[testIndex];
};