Index of maximum value within array - Node.js Array

Node.js examples for Array:Search

Description

Index of maximum value within array

Demo Code

/**/*  w ww.ja v a 2 s .  c o  m*/
 *
 * @param {Number} start
 *          start position in array
 * @param {Number} stop
 *          stop position in array
 *
 * @return {Number} index of maximum value within array
 *
 */
Array.prototype.max = function(start, stop) {
    start = (start != undefined)
        ? start
        : 0;
    stop = (stop != undefined)
        ? stop
        : this.length;
    var max = start;

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

Related Tutorials