当前位置:编程学习 > JS >>

JavaScript 求数组的最大值和最小值代码

原来Math.min和Math.max可以这么用。。

 代码如下 复制代码

function smallest(array){
  return Math.min.apply( Math, array );
}
function largest(array){
  return Math.max.apply( Math, array );
}
alert(smallest([0, 1, 2, 3]) == 0, "Locate the smallest value.");
alert(largest([0, 1, 2, 3]) == 3, "Locate the largest value.");

Math对象调用Math对象的min方法?不太理解。

 代码如下 复制代码

function smallest(){
  return Math.min.apply( Math, arguments );
}

function largest(){
  return Math.max.apply( Math, arguments );
}

alert(smallest(0, 1, 2, 3) == 0, "Locate the smallest value.");
alert(largest(0, 1, 2, 3) == 3, "Locate the largest value.");

补充:网页制作,js教程 
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,