«

求数组中最大值

时间:2023-3-12 16:37     作者:小诸葛     分类: JavaScript     正在检查是否收录...


方法一:

arr = [1, 3, 5, 79, 11];
            let max = arr[0];
            arr.forEach(function(v, i) {
                if (v > max) {
                    max = v;
                }
            })
console.log("最大值:", max)

方法二:

arr = [1, 3, 5, 79, 11]
            let max = arr[0];
            for (let i = 0; i < arr.length; i++) {
                if (arr[i] > max) {
                    max = arr[i]
                }
            }
            console.log("最大值", max)

最大值

推荐阅读:


扫描二维码,在手机上阅读