Skip to content

Instantly share code, notes, and snippets.

@lorepirri
Created June 30, 2017 12:20
Show Gist options
  • Save lorepirri/1882467a73042414d600733906bbe5e3 to your computer and use it in GitHub Desktop.
Save lorepirri/1882467a73042414d600733906bbe5e3 to your computer and use it in GitHub Desktop.
// 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