mhe*_*ers 27 javascript math comparison jquery comparison-operators
我正在尝试使用javascript来选择两个数字中较大的一个.我知道我可以写一个if语句,但我想知道是否有某种数学运算或某些东西可以提高效率.这是我用if语句做的方法:
if (a > b) {
c = a;
}
else {
c = b;
}
Run Code Online (Sandbox Code Playgroud)
BLS*_*lly 54
您正在寻找我认为的Max功能....
var c = Math.max(a, b);
Run Code Online (Sandbox Code Playgroud)
此功能也将使用两个以上的参数:
console.log(Math.max(4,76,92,3,4,12,9));
//outputs 92
Run Code Online (Sandbox Code Playgroud)
如果你有一个未知长度的列表来运行max,你可以使用apply
...
var arrayOfNumbers = [4,76,92,3,4,12,9];
console.log(Math.max.apply(null, arrayOfNumbers));
//outputs 92
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
19324 次 |
最近记录: |