手写的获取最大值的方法如果比较的数据都是负数就有问题了
来源: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
00 -
双越
2020-02-25
没啥问题吧
max(-10,-20,-5) // -5
052020-04-06
相似问题