【发布时间】:2012-02-09 13:13:01
【问题描述】:
当我在javascript数学最小和最大函数的参数中输入一个数组时,它会返回正确的值:
console.log( Math.min( 5 ) ); // 5
console.log( Math.max( 2 ) ); // 2
var array = [3, 6, 1, 5, 0, -2, 3];
var minArray = Math.min( array ); // -2
var maxArray = Math.max( array ); // 6
但是,当我使用不带参数的函数时,它会返回错误的答案:
console.log( Math.min() ); // Infinity
console.log( Math.max() ); // -Infinity
这个返回false:
console.log( Math.min() < Math.max() );
为什么要这样做?
【问题讨论】:
-
你用的是什么浏览器?
Math.min/max不适用于数组。 -
Chrome 版本:16.0.912.75 m
-
我也是,你确定你有
-2和6吗?他们应该是NaN -
为什么不带任何参数调用
Math.Min或Math.Max? -
看看wtfjs.com,第一项。
标签: javascript math max min