美文网首页
获取多个数字中的最大值

获取多个数字中的最大值

作者: 泡杯感冒灵 | 来源:发表于2020-07-27 22:02 被阅读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
}

相关文章

网友评论

      本文标题:获取多个数字中的最大值

      本文链接:https://www.haomeiwen.com/subject/ftwtrktx.html