创建由每个子阵列不起作用的最大值的数组如预期 [英] Creating an array consisting of the largest values of each sub-array does not work as expected

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

问题描述

查找每个子阵列的数量最多,然后使这些最大号的数组。 [[4,5,1,3],[13,27,18,26] [32,35,37,39],[1000,1001,857,1]

我写了一些code,我想不出有什么不妥的地方。也许Array.push()方法不起作用或者for循环。

 函数largestOfFour(ARR){
    VAR主= [];
    对于(K = 0; K< arr.length; k ++){
       VAR长= 0;
         对于(i = 0; I<常用3 [K] .length;我++){
            如果(ARR [k]的[1]  - ;长){
                改编[k]的[I] =长;
            }
            main.push [长]
        }
    }
    返回主
}

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

解决方案

现在的问题是在内部循环,当你试图找到每个阵列的最大值。在外部循环的每次迭代中,你应该重置长= ARR [k]的[0] 。自的最大值可小于0。注意,这期望所有子阵列具有至少一个物品它不应该被重置为0。

所指出的@ edc65,申报应该出现在函数的开始,使之清楚,,因为所有的局部变量,有一个函数的范围。


您只需要每个子阵一个值。因此,你应该为外循环的每个迭代增加一个值( main.push 应在外环)。事情是这样的现在,您要添加每个子数组元素一个值。


在if语句,你的任务是反转。它应该是

 长= ARR [k]的[I];
 

和条件也反转。 存储每个子阵列的最大值。因此,您更新,如果你找到一个值大于

 如果(ARR [k]的[I]>长){
    长=改编[k]的[I];
}
 


当推入阵列中使用括号,括号内不:

  main.push(长);
 

括号内调用的方法。支架是在对象访问属性。

最后code

 函数largestOfFour(ARR){
    VAR主= [];
    VAR长;
    对于(K = 0; K< arr.length; k ++){
       长=改编[k]的[0];
         对于(i = 0; I<常用3 [K] .length;我++){
            如果(ARR [k]的[I]>长){
                长=改编[k]的[I];
            }
        }
        main.push(长);
    }
    返回主;
}
 


数学。最高 方法

您可以使用 Math.max 来简化你的code

 函数largestOfFour(ARR){
    VAR主= [];
    对于(K = 0; K< arr.length; k ++){
        VAR长= Math.max.apply(空,编曲[K]);
        main.push(长);
    }
    返回主;
}
 

根据 @ BillyMoon的和的 @图莎尔的答案,这可以进一步简化为一个<一href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map?redirectlocale=en-US&redirectslug=JavaScript%2FReference%2FGlobal_Objects%2FArray%2Fmap"相对=nofollow> Array.map 调用。

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]]

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]], "");

解决方案

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.

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.


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.


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

long = arr[k][i];

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.

Final code

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 method

You can use Math.max to simplify your code

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;
}

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

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

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