array-like/reduce

View on GitHub
src/argmax.js

Summary

Maintainability
A
3 hrs
Test Coverage
/**
 * Argmax.
 *
 * @param {Function} compare
 * @param {ArrayLike} a
 * @param {number} i
 * @param {number} j
 * @return {number}
 */
const argmax = (compare, a, i, j) => {
    if (i >= j) {
        return undefined;
    }

    let argmaxSoFar = i;
    let maxSoFar = a[argmaxSoFar];

    for (++i; i < j; ++i) {
        const candidate = a[i];

        if (compare(candidate, maxSoFar) > 0) {
            argmaxSoFar = i;
            maxSoFar = candidate;
        }
    }

    return argmaxSoFar;
};

export default argmax;