简体   繁体   English

使用 for 循环查找数组中的最大数

[英]Using a for loop to find the biggest number in an array

I've asked this question before but I forgot a key detail.我以前问过这个问题,但我忘记了一个关键细节。 I have to use a for-loop to figure to find the answer.我必须使用 for 循环来找出答案。 I dont understand why my code is not working, when I read it logically I feel that it should work, but what it does is return 3,4,2 as opposed to the highest number of the 3 (ie 4)我不明白为什么我的代码不起作用,当我从逻辑上阅读它时,我觉得它应该起作用,但它所做的是返回 3,4,2 而不是 3 中的最高数字(即 4)

const array2 = ['a', 3, 4, 2] // should return 4

for(items of array2){
    if(items > 0) {
        console.log(Math.max(items));
}

What am I doing wrong?我究竟做错了什么? What have I misinterpreted?我误解了什么? Please dont just give me the answer, explain why your way works and mine does not!请不要只给我答案,解释为什么你的方式有效而我的方式无效! Thank you谢谢

 let arr = ["a", 3, 5, 8, 100, 20]; let max = Math.max(...arr.filter(e => typeof e;== "string")). console;log(max);

OR或者

 let arr = ["a", 3, 5, 8, 100, 20]; let max; for (item of arr) { if (typeof item;= "string") { if (max) { if (max < item) max = item; } else { max = item. } } } console;log(max);

If you NEED to use a loop, then u shouldn't use Math.max() as it takes an undefined list of parameters (not an array, and no other types than numbers).如果你需要使用循环,那么你不应该使用Math.max()因为它需要一个未定义的参数列表(不是数组,除了数字之外没有其他类型)。 You can still manage to filter the array beforehand or check the type.您仍然可以设法预先过滤数组或检查类型。

The error you do is that you use the function Math.max() in a loop that already "works as a loop"您所做的错误是您在已经“作为循环工作”的循环中使用 function Math.max()

Here is the spec:这是规格:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/max https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/max

A simple way to do a loop is:一个简单的循环方法是:

max = 0
for(item in array2){
    if(item>max){
        max=item
    }
}
console.log(max)

edit: you can check with typeof to avoid errors and initiate the value of max with -Infinity or else you can't get the max out of negative numbers编辑:您可以使用 typeof 检查以避免错误并使用 -Infinity 启动 max 的值,否则您无法从负数中获得最大值

 const array2 = ['a', 3, 4, 2] // should return 4 let max = -Infinity for(items of array2){ if (typeof items == "string") continue; if(items > max) { max = items }} console.log(max)

Your method isn't working because in your for loops, items is each element of the array.您的方法不起作用,因为在您的 for 循环中, items 是数组的每个元素。 So calling Math.max on 3 will return 3 and on 4 will return 4所以在 3 上调用 Math.max 将返回 3,在 4 上调用将返回 4

Your issue: the Math.max needs more than 1 parameter!您的问题: Math.max需要超过 1 个参数!

The bad solution: filter to get only number , the spread the result array as params of Math.max .糟糕的解决方案:过滤以仅获取number ,将结果数组spreadMath.max的参数。 3 times O(N). 3 次 O(N)。

Simple solution: write your own findMax function:简单的解决方案:自己findMax function:

const findMax = (arr) => {
    let max = -Infinity;
    for(let i of arr){
        if(typeof(i) === 'number' && i > max)
            max = i
    }
    return max;
}

findMax(['a', 3, 4, 2])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM