Skip to content

Instantly share code, notes, and snippets.

@dxc04
Created May 29, 2020 07:56
Show Gist options
  • Save dxc04/72df0bb9f41173138d08ba4900a7a225 to your computer and use it in GitHub Desktop.
Save dxc04/72df0bb9f41173138d08ba4900a7a225 to your computer and use it in GitHub Desktop.
Stocks array challenge
function stocksArrayChallenge(arr) {
const length = arr.length;
const minIndex = arr.reduce((min, cur, i, arr) => cur < arr[min] ? i : min, 0);
const maxIndex = arr.reduce((max, cur, i, arr) => cur > arr[max] ? i : max, 0);
if (length < 2 || (length == 2 && maxIndex < minIndex) ) {
return -1;
}
if (maxIndex > minIndex) {
return arr[maxIndex] - arr[minIndex];
}
else {
return stocksArrayChallenge(arr.slice(minIndex, length))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment