How to get the maximum value of multiple numbers?
- method one:
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;
}
- Method 2:
Use the API directly:
Math.max(10,20,50,30);//求最大值 50
Math.min(10,20,50,30);//求最小值10