最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Javascript - find max and min values using loop and array, -InfinityInfinity - Stack Overflow

programmeradmin4浏览0评论

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.

//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);

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.

//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);
Share Improve this question asked Oct 9, 2015 at 16:55 Jakub MazurkiewiczJakub Mazurkiewicz 671 gold badge1 silver badge5 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 2

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.

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.

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

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

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

发布评论

评论列表(0)

  1. 暂无评论