Javascript Array max() method

Description

Javascript Array max() method

Array.max = function( array ){
    return Math.max.apply( Math, array );
};

Javascript Array max()




Array.prototype.max = function() {
  return Math.max(...this);
}

Javascript Array max()

Array.prototype.max = function() {
  return Math.max(...this);
}
console.log([1,2,3,4,5].max());//from w  ww .  j  av  a 2  s .  com

Javascript Array max()

Array.prototype.max = function() {
  return Math.max.apply(null, this);
};

Array.prototype.min = function() {
  return Math.min.apply(null, this);
};

var a = [1,2,3];/* w w w  . j ava  2 s .  c o m*/
console.log(a.max());
console.log(a.min());

Javascript Array max()

Array.prototype.max = function() {
  return Math.max.apply(null, this);
};

Javascript Array max()

// make max method fluent on instances of Array
// [100, -100, 1000, -1000, 0].max() => 1000

Array.prototype.max = function() {
  return Math.max.apply(null, this)
}

Javascript Array max()

Array.prototype.max = function() {
    return Math.max.apply(null, this)
};

Javascript Array max()

Array.prototype.max = function () {
  return this.reduce((a, b) => Math.max(a, b), Number.NEGATIVE_INFINITY)
}

Javascript Array max()

/**//from   w ww  .  j  ava  2 s.  c  o  m
 * Returns the maximum value of the array
 * @return the maximum value of the array
 */
Array.prototype.max = function() {
  return Math.max.apply(Math, this);
};

Javascript Array max()

Array.prototype.max = function() {
    return Math.max.apply(null, this);
}


var arr = [1, 2, 3]
console.log(arr.max())/*  ww w  . j a va 2  s  .  c om*/


function findLongestWord(str) {
    var array = str.split(' ');
    var longest = 0;
    for (var i = 0; i < arr.length; i++) {
        if (array[i].length > longest) {
            longest = array[i].length;
        }
    }

    return longest;
}

Javascript Array max()

function array_max( ){
   var i, max = this[0];
   for (i = 1; i < this.length; i++) {
      if (max < this[i])
         max = this[i];//from w w  w.  j av a  2s .c  om
   }
   return max;
}

Array.prototype.max = array_max;

var x = [ 1, 2, 3, 4, 5, 6]
var y = x.max( );
alert(this.y);

Javascript Array max()

function array_max( )
{
    var i, max = this[0];
    for (i = 1; i < this.length; i++)
    {//from   www.  ja v a2  s  .co m
        if (max < this[i])
        {
            max = this[i];
        }
    }
    return max;
}
Array.prototype.max = array_max;
var x = new Array(1, 2, 3, 4, 5, 6);
var y = x.max();

Javascript Array max()

Array.prototype.max = function(){
    var max = this[0];
    this.forEach(function(element, index, array){
        if(element > max){
            max = element;//from  w  ww .j av  a2  s  .  co  m
        }
    })
    return max;
}

var arr1 = [3,6,9,1,5,0,11];

console.log(arr1.max());

Javascript Array max()

//Return Largest Numbers in Arrays

Array.prototype.max = function(){
  return Math.max.apply(null, this);
};

function largestOfFour(arr) {
  // You can do this!
  var myArr=[];/*from ww w.  j  ava 2s.  c  o m*/
  for(var i=0; i<arr.length;i++){
    myArr.push(arr[i].max());
  }
  return myArr;
}

largestOfFour([[4, 5, 1, 3], [13, 27, 18, 26], [32, 35, 37, 39], [1000, 1001, 857, 1]]);
//result: [5, 27, 39, 1001]

Javascript Array max()

Array.prototype.max = function() {
  var r = null;//from  ww  w. ja  v a 2  s. com
  for (var i = 0; i < this.length; i++) {
    if (r === null || this[i] > r) {
      r = this[i];
    }
  }
  return r;
};

Javascript Array max()

Array.prototype.max = function () {
  return this.reduce((a, b) => (a > b ? a : b), Number.MIN_SAFE_INTEGER);
};

Javascript Array max()

Array.prototype.max = function(){
  var max=this[0];
  this.forEach(function(v){max=(v>max)?v:max;});
  return max;/*from www  .j a  v  a2 s  .c o m*/
};

Javascript Array max()

Array.prototype.max = function() {
  return Math.max.apply(null, this);
};

function largestOfFour(arr) {
  return arr.map(function(a){
    return a.max()
  });/*from w w  w . ja  v a  2  s.c om*/
}

//without polluting array
function largestOfAll(arr) {
  return arr.map(function(a){
    return Math.max.apply(null, a);
  });
}

console.log(largestOfFour([[4, 5, 1, 3], [13, 27, 18, 26], [32, 35, 37, 39], [1000, 1001, 857, 1]]));
console.log(largestOfAll([[4, 5, 1, 3], [13, 27, 18, 26], [32, 35, 37, 39], [1000, 1001, 857, 1]]));

Javascript Array max()

Array.prototype.max = function(){
 return Math.max.apply({}, this);
};

Javascript Array max()

Array.prototype.max = function() {
    return Math.max.apply(null, this);
}

Javascript Array max()

Array.prototype.max = function(){
    var i, max = this[0];
    for (i = 1; i < this.length; i++){
        if (max < this[i])
            max = this[i];/*from   w w w .j  a  v  a 2  s. c o m*/
    }
    return max;
};

Javascript Array max()

Array.prototype.max = function() {
 return this.reduce(function(p, v) {
  return (p > v ? p : v);
 });/*from  w  ww  .j  a va  2  s  .  com*/
}

Javascript Array max()

/* Functions to work with arrays more easily */

Array.maxProp = function (array, prop) {
  var values = array.map(function (el) {
    return el[prop];
  });//  w w w . j av a  2  s . co  m
  return values.max()
};

Array.sumProp = function (array, prop) {
  var values = array.map(function (el) {
    return el[prop];
  });
  return values.reduce(function (a, b) { return a + b; }, 0);
};

Array.prototype.max = function() {
  return Math.max.apply(null, this);
};

Array.avgProp = function (array, prop)?{
  var values = array.map(function (el) {
    return el[prop];
  });
  return values.avg()
}

Array.prototype.avg = function(){
    var l=this.length
    var t=0;
    for(var i=0;i<l;i++){
        t+=this[i];
    }
    return Math.floor(t/l);
}

Javascript Array max()

// find the max of an array
Array.prototype.max = function() {
    return Math.max.apply(null, this);
};

Javascript Array max()

Array.prototype.max = function(){
 return this.reduce(function(p,n){ 
  if(n > p || p === null){ 
   return n;/*  w ww .  j  a v a2  s.c om*/
  } else {
   return p;
  }
 },null);
};

Javascript Array max()

Array.prototype.max = function () {
    return this.reduce(function(previousValue, currentValue) {
        return previousValue > currentValue ? previousValue : currentValue;
    });/*from   w w w  . j  a v a 2s .c om*/
};

Javascript Array max()

/*/*from  w  ww.  j  a  v  a 2  s.  c o m*/
 * Returns the maximum element of the array.
 */
Array.prototype.max = function ()
 {
 return Math.max.apply (null, this);
 };



PreviousNext

Related