Index of minimum value within array - Node.js Array

Node.js examples for Array:Search

Description

Index of minimum value within array

Demo Code


/**/*from w  w  w . ja  va  2s. co m*/
 *
 * @param {Number} start
 *          start position in array
 * @param {Number} stop
 *          stop position in array
 *
 * @return {Number} index of minimum value within array
 *
 */
Array.prototype.min = function(start, stop) {
    start = (start != undefined)
        ? start
        : 0;
    stop = (stop != undefined)
        ? stop
        : this.length;
    var min = start;

    for (var i=(start+1); i<stop; i++) if (this[i] < this[min]) min = i;
    return min;
};

Related Tutorials