获取多个数字中的最大值

2020-07-27  本文已影响0人  泡杯感冒灵

方法1:直接利用Math的max方法

Math.max(10,20,30,40)  // 40

方法2:自己写

// 不考虑都是负数的情况
function max(){
    const nums = Array.prototype.slice.call(arguments)  //把参数集合变为数组方便遍历
    let max = 0
    nums.forEach(n => {
        if(n > max){
            max = n
        }
    })
    return max
}
上一篇 下一篇

猜你喜欢

热点阅读