- 双重for循环
for (let i = 0; i < arr.length - 1; i++) {
for (let j = 0; j < arr.length - 1 - i; j++) {
if(arr[j] < arr[j + 1]) {
let temp = arr[j]
arr[j] = arr[j + 1]
arr[j + 1] = temp
}
}
}
console.log(arr[arr.length - 1])
- ES6 -- reduce
let res = arr.reduce((pre, curr, index) => {
return pre > curr ? curr : pre
})
console.log(res);
- ES6 -- sort()
arr.sort((a,b) => b-a)
console.log(arr[arr.length - 1])
- Math.min.apply()
console.log(Math.min.apply(null, arr))