创建由每个子数组的最大值组成的数组无法按预期工作 [英] Creating an array consisting of the largest values of each sub-array does not work as expected

查看:36
本文介绍了创建由每个子数组的最大值组成的数组无法按预期工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在每个子数组中找出最大的数,然后将这些最大的数组成一个数组.[[4, 5, 1, 3], [13, 27, 18, 26], [32, 35, 37, 39], [1000, 1001, 857, 1]]

Find the largest number in each of the sub-array and then make an array of those largest numbers.[[4, 5, 1, 3], [13, 27, 18, 26], [32, 35, 37, 39], [1000, 1001, 857, 1]]

我写了一些代码,但我无法弄清楚它有什么问题.也许 Array.push() 方法不起作用,或者 for 循环.

I wrote some code, and I can't figure out what is wrong with it. Maybe the Array.push() method doesn't work or perhaps the for loops.

function largestOfFour(arr) {
    var main = [];
    for(k=0;k<arr.length;k++){
       var long= 0;
         for(i=0;i<arr[k].length;i++){
            if(arr[k][i]<long) {
                arr[k][i] = long;
            }
            main.push[long];
        }
    }
    return main
}

largestOfFour([[4, 5, 1, 3], [13, 27, 18, 26], [32, 35, 37, 39], [1000, 1001, 857, 1]], "");

推荐答案

问题出在内部循环上,当您试图找到每个数组的最大值时.在外循环的每次迭代中,您应该重置 long = arr[k][0].不应将其重置为 0,因为最大值可能小于 0.请注意,这要求所有子数组至少具有一项.

The problem is on the inner loop, when you try to find the max value for each array. On each iteration of the outer loop, you should reset long = arr[k][0]. It should not be reset to 0 since the max value may be smaller than 0. Note that this expects all subarrays to have at least one item.

正如@edc65 所指出的,long 的声明应该出现在函数的开头,以表明 long 作为所有局部变量,都有一个函数范围.

As noted by @edc65, the declaration of long should occur at the start of the function to make it clear that long, as all local variables, has a function scope.

每个子数组只需要一个值.因此,您应该为外循环的每次迭代添加一个值(main.push 应该在外循环中).按照目前的方式,您为每个子数组元素添加一个值.

You only want one value per subarray. Therefore you should be adding one value for each iteration of the outer loop (main.push should be in the outer loop). The way it is currently, you are adding one value per subarray element.

在 if 语句中,您的赋值是倒置的.应该是

In the if statement, your assignment is inverted. It should be

long = arr[k][i];

而且条件也颠倒了.long 存储每个子数组的最大值.因此,如果您发现一个值大于,则更新它:

And the condition is also inverted. long stores the max value for each subarray. Therefore, you update it if you find a value greater than it:

if(arr[k][i]>long) {
    long = arr[k][i];
}

<小时>

推入数组时使用括号,而不是括号:


When pushing into the array use parenthesis, not brackets:

main.push(long);

括号用于调用方法.方括号用于访问对象中的属性.

Parenthesis are for calling methods. Brackets are for accessing properties in an object.

function largestOfFour(arr) {
    var main = [];
    var long;
    for(k=0;k<arr.length;k++){
       long = arr[k][0];
         for(i=0;i<arr[k].length;i++){
            if(arr[k][i]>long) {
                long = arr[k][i];
            }
        }
        main.push(long);
    }
    return main;
}

<小时>

Math.max 方法

您可以使用 Math.max 来简化您的代码

function largestOfFour(arr) {
    var main = [];
    for(k=0;k<arr.length;k++){
        var long = Math.max.apply(null, arr[k]);
        main.push(long);
    }
    return main;
}

根据 @BillyMoon's@Tushar 的 答案,这可以进一步简化为 Array.map 调用.

As per @BillyMoon's and @Tushar's answers, this can be further simplified to an Array.map call.

这篇关于创建由每个子数组的最大值组成的数组无法按预期工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆