求数组最大值

150 阅读1分钟
let arr = [10,20,30,40,100]

1. 擂台思想

 let arr = [10, 20, 30, 40, 100]
 
 let max = -Infinity
    
    for (let i = 0; i < arr.length; i++) {
      if (arr[i] > max) {
        max = arr[i]
      }
    };
    console.log(max);

2. Math

let arr = [10, 20, 30, 40, 100]

 let max = Math.max.apply(arr, arr)
 console.log(max);

3.万能检测数据类型

这个call就是为了改变this的指向
Object.prototype.toString.call()  // [object,数据类型]