Created
June 30, 2017 12:20
-
-
Save lorepirri/1882467a73042414d600733906bbe5e3 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
// how to get the index of the max/min element in an array | |
let scores = [2, 0, -1, 7, 1]; | |
console.clear(); | |
let scoreIndex = scores.reduce( | |
(iMax, x, i, arr) => x > arr[iMax] ? i : iMax, 0); | |
console.log('calc max index', scoreIndex); | |
scoreIndex = scores.reduce( | |
(iMin, x, i, arr) => x < arr[iMin] ? i : iMin, 0); | |
console.log('calc min index', scoreIndex); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment