如何获取多个数字中的最大值
手写函数
js
// 返回最大值
function max() {
// Arguments(6) [1, 2, 3, 7, 6, 5, callee: ƒ, Symbol(Symbol.iterator): ƒ]
console.log(arguments);
// 相当于 Array.prototype.slice.apply(arguments, [0, arguments.length])
// 或者 const nums = Array.prototype.slice.call(arguments)
const nums = Array.prototype.slice.call(arguments, 0, arguments.length);
let max = 0;
nums.forEach((n) => {
if (n > max) {
max = n;
}
});
return max;
}
console.log(max(1, 2, 3, 7, 6, 5)); // 7
也可以用
js
console.log(Math.max(1, 2, 3, 6, 5, 4)); // 6