// 最小值 Array.prototype.min = function () { let min = this[0]; let len = this.length; for (let i = 1; i < len; i++) { if (this[i] < min) min = this[i] } return min } // 最大值 Array.prototype.max = function () { let max = this[0]; let len = this.length; for (let i = 1; i < len; i++) { if (this[i] > max) max = this[i] } return max } // 结果 console.log(arr.min()); // 1 console.log(arr.max()); // 9
// 最小值 Array.prototype.min = function () { returnthis.reduce((pre, cur) => { return pre < cur ? pre : cur }) } // 最大值 Array.prototype.max = function () { returnthis.reduce((pre, cur) => { return pre > cur ? pre : cur }) } // 结果 console.log(arr.min()); // 1 console.log(arr.max()); // 9