Javascript - 使用循环和数组查找最大值和最小值,-Infinity/Infinity

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33043562/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 16:03:56  来源:igfitidea点击:

Javascript - find max and min values using loop and array, -Infinity/Infinity

javascriptarraysloopsnumbers

提问by Jakub Mazurkiewicz

Could someone explain to me how does this work. In find highest number method every number is larger than -Infinity. Why does it take the highest number? Same with lowest number finder, how does it selects lowest number, all of the numbers are smaller than Infinity.

有人可以向我解释这是如何工作的。在查找最高数方法中,每个数都大于 -Infinity。为什么取最高的数字?和最小数查找器一样,它是如何选择最小数的,所有的数都小于无穷大。

//find highest number

var Numbers = [1, 2, 101, 45, 55, 1443];
var l = Numbers.length;
var max = -Infinity;
var i;
for (i = 0; l > i; i++) {

    if (Numbers[i] > max) {

        max = Numbers[i];

    }

}

console.info(max);

//find lowest number

var Numbers = [1, 2, 101, 45, 55, 1443];
var l = Numbers.length;
var max = Infinity;
var i;
for (i = 0; l > i; i++) {

    if (Numbers[i] < max) {

        max = Numbers[i];

    }

}

console.info(max);

采纳答案by toskv

The key is here.

关键在这里。

if (Numbers[i] > max) {

    max = Numbers[i];

}

If the current number is greater than the current maximum number then we make that the max number. And we continue until we go trough the entire array.

如果当前数量大于当前最大数量,那么我们将其设为最大数量。我们继续,直到我们遍历整个阵列。

Starting with -Infinity ensures that any value in the array will be greater or equal to it.

以 -Infinity 开头可确保数组中的任何值都大于或等于它。

For the minimum it's the same but we always keep the smallest value we find in the list.

对于最小值,它是相同的,但我们始终保留在列表中找到的最小值。

Infinity and -Infinity are values javascript provides, they are greater or smaller than any other value of type Number. You can find more about them here.

Infinity 和 -Infinity 是 javascript 提供的值,它们大于或小于Number类型的任何其他值。您可以在此处找到有关它们的更多信息。

You could debug your code and see exactly what's happening step by step. Check this linkon how to do that in Chrome.

您可以调试您的代码并逐步查看正在发生的事情。查看此链接了解如何在 Chrome 中执行此操作。

回答by Sarjan Desai

Using console, you can see that for lowest number ifcondition only true for once when 1is less than Infinity. Other times it always return false because all other numbers are less then 1so here the logic is if number from array is lowerthan max then it store else loop continues to looping.

使用控制台,您可以看到,对于最低数字if条件,当1小于时,只成立一次Infinity。其他时候它总是返回 false 因为所有其他数字都小于1所以这里的逻辑是如果数组中的数字低于max 那么它存储 else 循环继续循环。

Same for highest number.

最高数字相同。

//find highest number
var Numbers = [1, 2, 101, 45, 55, 1443];
var l = Numbers.length;
var max = -Infinity;
var i;
for (i = 0; l > i; i++) {
  if (Numbers[i] > max) {
    console.log(i + ' max number' + Numbers[i]);
    max = Numbers[i];
  }
}
console.info(max);

//find lowest number
var Numbers = [1, 2, 101, 45, 55, 1443];
var l = Numbers.length;
var max = Infinity;
var i;
for (i = 0; l > i; i++) {
  if (Numbers[i] < max) {
    console.log(i + ' lowest number' + Numbers[i]);
    max = Numbers[i];
  }
}
console.info(max);