JavaScript算法练习:寻找数组中的最大值

2018-03-0906:07:10WEB前端开发Comments2,555 views字数 372阅读模式

寻找数组中的最大值
右边大数组中包含了4个小数组,分别找到每个小数组中的最大值,然后把它们串联起来,形成一个新数组。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/gcs/1348.html

提示:你可以用for循环来迭代数组,并通过arr[i]的方式来访问数组的每个元素。
Answer:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/gcs/1348.html

function largestOfFour(arr) {
    // You can do this!
    var maxNum = [];
    for (i = 0; i < arr.length; i++) {
        var temp = arr[i][0];
        for (j = 1; j < arr[i].length;j++) {
            if (arr[i][j] > temp) {
                temp = arr[i][j];
            }
        }
        maxNum[i] = temp;
    }
    return maxNum;
}
largestOfFour([[4, 5, 1, 3], [13, 27, 18, 26], [32, 35, 37, 39], [1000, 1001, 857, 1]]);
文章源自菜鸟学院-https://www.cainiaoxueyuan.com/gcs/1348.html
  • 本站内容整理自互联网,仅提供信息存储空间服务,以方便学习之用。如对文章、图片、字体等版权有疑问,请在下方留言,管理员看到后,将第一时间进行处理。
  • 转载请务必保留本文链接:https://www.cainiaoxueyuan.com/gcs/1348.html

Comment

匿名网友 填写信息

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定