const arr =[33,4,9]let max = arr[0];for(let i =0; i < arr.length -1; i++){
max = max < arr[i+1]? arr[i+1]: max
}let min = arr[0];for(let i =0; i < arr.length -1; i++){
min = min > arr[i +1]? arr[i +1]: min
}
console.log(min, max);
二、sort方法
const arr =[33,4,9]const s = arr.sort((a, b)=>{return a - b })
console.log(s[0], s[arr.length -1]);
三、Math
原始Math(其中需要放入一轮数字)
const s = Math.min(33,4,9)
ES5,使用apply()
const arr =[33,4,9]const s = Math.min.apply(null, arr)const m = Math.max.apply(null, arr)// 相当于Math.min(arr[0], arr[1]...)
console.log(s, m);
ES6
const arr =[33,4,9]const s = Math.min(...arr)const m = Math.max(...arr)
console.log(s, m);
一、for循环const arr = [33, 4, 9]let max = arr[0];for (let i = 0; i < arr.length - 1; i++) { max = max < arr[i+1] ? arr[i+1] : max}let min = arr[0]; for (let i = 0; i < arr.length - 1; i++) { min = min > arr[i + 1] ? arr[i + 1]