Nodejs Array Max max()

Here you can find the source of max()

Method Source Code

var Canvas = require('../')
  , canvas = new Canvas(50, 50)/*from  w w w. j  a  v  a2 s.com*/
  , ctx = canvas.getContext('2d');

process.on('SIGINT', function(){
  ctx.reset();
  process.nextTick(function(){
    process.exit();
  });
});

Array.prototype.max = function(){
  return this.sort(function(a, b){
    return a - b;
  }).pop();
};

function rand() {
  return Math.random() * 5 | 0;
}

ctx.hideCursor();

setInterval(function(){
  var data = [rand(), rand(), rand(), rand(), rand(), rand()]
    , max = data.max()
    , x = 3;

  ctx.clearRect(0, 0, canvas.width, canvas.height);
  data.forEach(function(n){
    var y = 20
      , w = 8
      , h = 20 * (n / max);

    ctx.fillStyle = 'blue';
    ctx.fillRect(x, y - h + 3, w, h);
    ctx.fillStyle = 'white';
    ctx.fillText(n.toString(), x + 3, y + 1);
    x += 9;
  });
}, 1000 / 6);

Related

  1. max()
    Array.prototype.max = function() {
      return this.reduce(function(p, v) {
        return (p > v ? p : v);
      });
    
  2. max()
    Array.prototype.max = function(){
        var i, max = this[0];
        for (i = 1; i < this.length; i++){
            if (max < this[i])
                max = this[i];
        return max;
    };
    
  3. max()
    Math.clamp = function(val, min, max) {
        return Math.max(Math.min(val, max), min);
    };
    Array.prototype.max = function() {
        return Math.max.apply(null, this);
    Array.prototype.min = function() {
        return Math.min.apply(null, this);
    
  4. max()
    maxNum([1,2,3]);    
    maxNum([10,3,10,4]);  
    maxNum([-5,100]);    
    function maxNum(arr) {
      var max = arr[0];
      for(var i = 1; i < arr.length; i++) {
        if(arr[i] > max) {
          max = arr[i];
      return max;
    function maxNum(arr) {
      var len = arr.length; 
      var max = -Infinity;
      while (len--) {
        if (arr[len] > max) {
          max = arr[len];
      return max;
    };
    var arr = [1,3,5];
    Array.prototype.max = function() {
      return Math.max.apply(null, this);
    console.log(arr.max());
    
  5. max()
    Array.prototype.max = function() {
      return Math.max.apply(null, this);
    };
    Array.prototype.min = function() {
      return Math.min.apply(null, this);
    };
    Array.prototype.count = function(){
      return this.length;
    };
    ...
    
  6. max()
    Array.prototype.max = function () {
        return this.reduce(function(previousValue, currentValue) {
            return previousValue > currentValue ? previousValue : currentValue;
        });
    };
    
  7. 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;
    };
    
  8. 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;
    
  9. 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];