greatestIndex.js 470 B

12345678910111213141516171819
  1. import ascending from "./ascending.js";
  2. import maxIndex from "./maxIndex.js";
  3. export default function greatestIndex(values, compare = ascending) {
  4. if (compare.length === 1) return maxIndex(values, compare);
  5. let maxValue;
  6. let max = -1;
  7. let index = -1;
  8. for (const value of values) {
  9. ++index;
  10. if (max < 0
  11. ? compare(value, value) === 0
  12. : compare(value, maxValue) > 0) {
  13. maxValue = value;
  14. max = index;
  15. }
  16. }
  17. return max;
  18. }