Created
May 29, 2020 07:56
-
-
Save dxc04/72df0bb9f41173138d08ba4900a7a225 to your computer and use it in GitHub Desktop.
Stocks array challenge
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 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