码迷,mamicode.com
首页 > 其他好文 > 详细

求数组中最大值,最小值

时间:2014-10-09 15:45:23      阅读:186      评论:0      收藏:0      [点我收藏+]

标签:blog   io   ar   sp   div   on   cti   log   bs   

Array.prototype.max = function(){   //最大值
 return Math.max.apply({},this)
}

Array.prototype.min = function(){   //最小值
 return Math.min.apply({},this)
}

var myMax = [1,2,3].max()// => 3
var myMin = [1,2,3].min()// => 1

console.log(myMin,myMax);
var _getMin = function(arr){         //最小值
    return Math.min.apply(Math,arr);      
}    
var _getMax = function(arr){         //最大值
    return Math.max.apply(Math,arr);
}

var tempArr=[1,2,3,4];
var myMin = _getMin(tempArr);   //==>1
var myMax = _getMax(tempArr);   //==>4
console.log(myMin,myMax);      // 1,4

求数组中最大值,最小值

标签:blog   io   ar   sp   div   on   cti   log   bs   

原文地址:http://www.cnblogs.com/liujinyu/p/4012976.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!