如何获取多个数字的最大值?
- 方法一:
function max(){
const nums = Array.prototype.slice.call(arguments);//把输入参数变成数组,方便用forEach遍历
let max = 0;
nums.forEach( n = > {
if (n > max){
max = n; //比较每个数和max的大小,要是比max大就把它赋值给max 保证max是最大值
}
})
return max;
}
- 方法二:
直接用API:
Math.max(10,20,50,30);//求最大值 50
Math.min(10,20,50,30);//求最小值10