Nodejs Array Max max()

Here you can find the source of max()

Method Source Code

// Here used the cycle 'for' as it is possible of stack oveflow
// when function Math.max.apply is used (error occurs only with
// array of huge sizes)
Array.prototype.max = function() {
  var max = this[0];
  for (var i = 0; i < this.length; i++) if(this[i] > max) max = this[i];

  return max;/*from ww w.j a va  2 s.  c  o  m*/
};

// And here too
Array.prototype.min = function() {
  var min = this[0];
  for (var i = 0; i < this.length; i++) if(this[i] < min) min = this[i];

  return min;
};

Related

  1. max()
    Array.prototype.max = function () {
        return this.reduce(function(previousValue, currentValue) {
            return previousValue > currentValue ? previousValue : currentValue;
        });
    };
    
  2. max()
    Array.prototype.max = function() {
      var max = this[0];
      for(var i=1; i<this.length; i++) {
        if(max < this[i]) max = this[i];
      return max;
    };
    
  3. max()
    Array.prototype.max = function(){
      var ret=this[0];
      for(var i=1;i<this.length;i++){
        ret = ret>this[i]?ret:this[i];
      return ret;
    
  4. max()
    Array.prototype.max = function(){
      var a = this;
      var max = 0;
      for(var x = 1;x<a.length;x++)
        if(a[x]>a[max])
        max = x;
      return a[max];
    
  5. max()
    Array.prototype.max = function () {
        var max = this[0];
        var len = this.length;
        for (var i = 1; i < len; i++) if (this[i] > max) max = this[i];
        return max;
    
  6. max()
    Array.prototype.max = function() {
        var length = this.length;
        var max = this[0];
        for ( var i = 0; i < length; i++ )
            var elem = this[i];
            if ( elem > max ) max = elem;
        return max;
    
  7. max()
    Array.prototype.max = function() {
      var maxval = 0;
      for (var i=0; i<this.length;i++) {
        var val = Number(this[i]);
        if (!isNaN(val)) {
          maxval = Math.max(maxval, val);
      return maxval;
    ...
    
  8. max()
    Array.prototype.max = function() {
      var max = -Infinity;
      for (var i = 0; i < this.length; i++) {
        if (typeof this[i] === "number" && this[i] > max) {
          max = this[i];
      return max;
    };
    ...
    
  9. max()
    Array.prototype.max = function() {
      if (this.toNumsOnly().length == 0) return undefined;
      return Math.max.apply(null, this.toNumsOnly());
    };