Created
November 7, 2017 11:41
-
-
Save IGI-111/d45c2ff6f77eaa6280274a6392c0ff1a to your computer and use it in GitHub Desktop.
This file contains 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 stockPicker(market) { | |
let lowest_index = 0; | |
const lowest_previous_index = market.map((x, i) => { | |
if (x < market[lowest_index]) { | |
lowest_index = i; | |
} | |
return lowest_index; | |
}); | |
let best = { | |
high_index: 0, | |
low_index: 0, | |
profit: 0 | |
}; | |
market.forEach((x, i) => { | |
const current_profit = x - market[lowest_previous_index[i]]; | |
if (current_profit > best.profit) { | |
best = { | |
high_index: i, | |
low_index: lowest_previous_index[i], | |
profit: current_profit, | |
}; | |
} | |
}); | |
return [best.low_index, best.high_index]; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment