array-like/reduce

View on GitHub
src/argmin.js

Summary

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

    let argminSoFar = i;
    let minSoFar = a[argminSoFar];

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

        if (compare(candidate, minSoFar) < 0) {
            argminSoFar = i;
            minSoFar = candidate;
        }
    }

    return argminSoFar;
};

export default argmin;