admin管理员组文章数量:1356225
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
2 Answers
Reset to default 2The 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);
本文标签: Javascriptfind max and min values using loop and arrayInfinityInfinityStack Overflow
版权声明:本文标题:Javascript - find max and min values using loop and array, -InfinityInfinity - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1743966453a2569975.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论