Last active
July 7, 2016 15:17
-
-
Save kylebakerio/cdb413c4dc747729efc50c56b4ad0741 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function inAndOut(prices) { | |
let high = prices[0]; | |
let low = prices[0]; | |
let lowIndex = 0; | |
let bestGap = 0; | |
const bestIndexes = []; | |
const currentGap = () => high - low; | |
for (var i = 1; i < prices.length; i++) { | |
if (prices[i] < low) { | |
// new slope; set new low, reset high. | |
low = prices[i]; | |
lowIndex = i; | |
high = prices[i]; | |
} | |
else if (prices[i] > high) { | |
// new peak on current slope | |
high = prices[i]; | |
if (currentGap() > bestGap) { | |
// new best | |
bestGap = currentGap(); | |
bestIndexes[0] = lowIndex; | |
bestIndexes[1] = i; | |
} | |
} | |
} | |
return bestIndexes; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment