手写的获取最大值的方法如果比较的数据都是负数就有问题了

来源:18-10 面试讲解-9:如何获取最大值

qq_小江南_04298178

2020-02-25

手写的获取最大值的方法如果比较的数据都是负数岂不是获取到的最大值就是这一组数据中不存在的 0 了,
max 初始值我觉得应该从待比较的数据中任意取一个

function max (){
    const numArray = Array.prototype.slice.call(arguments)
    // let max = 0
    let max = numArray[0]
    numArray.forEach((item) => {
        if(item >max){
            max = item
        }
    })
    return max
}
写回答

2回答

双越

2020-04-01

function max1(){
    const numArray = Array.prototype.slice.call(arguments)
    // let max = 0
    let max = numArray[0]
    numArray.forEach((item) => {
        if(item >max){
            max = item        }
    })
    return max
}
max1(-10,-20,-5) // -5


0
0

双越

2020-02-25

没啥问题吧

max(-10,-20,-5) // -5


0
5
慕神9465694
回复
qq_慕运维6046394
取第一个元素是同学的建议啊,你看提问的同学
2020-04-06
共5条回复

一天时间高效准备前端技术一面 匹配大厂面试要求

针对时下面试高频考点,帮助新人js面试快速通关

4713 学习 · 1683 问题

查看课程