admin管理员组文章数量:1386693
I have developed a code in which adds values and at the end subtracts the smallest value based on the items you select in a form. The code works fine however the problem occurs when you unselect all the items and instead of displaying 0, it displays -Infinity. What do I do to this script to force it to display 0 instead of -Infinity?
// All selected prices are stored on a array
var prices = [];
// A function to remove a item from an array
function remove(array, item) {
for (var i = 0, len = array.length; i < len; i++) {
if (array[i] == item) {
array.splice(i, 1);
return true;
}
}
return false;
}
function calculateSectedDues(checkbox, amount) {
// We add or remove the price as necesary
if (checkbox.checked) {
prices.push(amount);
} else {
remove(prices, amount);
}
// We sum all the prices
var total = 0;
for (var i = 0, len = prices.length; i < len; i++)
total += prices[i];
// We get the lower one
var min = Math.min.apply(Math, prices);
// And substract it
total -= min;
// Please, don't access the DOM elements this way, use document.getElementById instead
document.grad_enroll_form.total.value = total;
}
</script>
I have developed a code in which adds values and at the end subtracts the smallest value based on the items you select in a form. The code works fine however the problem occurs when you unselect all the items and instead of displaying 0, it displays -Infinity. What do I do to this script to force it to display 0 instead of -Infinity?
// All selected prices are stored on a array
var prices = [];
// A function to remove a item from an array
function remove(array, item) {
for (var i = 0, len = array.length; i < len; i++) {
if (array[i] == item) {
array.splice(i, 1);
return true;
}
}
return false;
}
function calculateSectedDues(checkbox, amount) {
// We add or remove the price as necesary
if (checkbox.checked) {
prices.push(amount);
} else {
remove(prices, amount);
}
// We sum all the prices
var total = 0;
for (var i = 0, len = prices.length; i < len; i++)
total += prices[i];
// We get the lower one
var min = Math.min.apply(Math, prices);
// And substract it
total -= min;
// Please, don't access the DOM elements this way, use document.getElementById instead
document.grad_enroll_form.total.value = total;
}
</script>
Share
Improve this question
edited Apr 16, 2013 at 20:34
Jeremy Banks
130k88 gold badges358 silver badges381 bronze badges
asked Apr 16, 2013 at 20:32
user2288120user2288120
0
1 Answer
Reset to default 6Math.min()
with no arguments returns Infinity
, which is what happens when you call Math.min.apply(Math, prices)
with an empty prices
array.
Why not just detect the presence of an Infinity
minimum and reset it to zero?
// We get the lower one
var min = Math.min.apply(Math, prices);
// ** test for Infinity **
if(min == Infinity) { min = 0; }
// And substract it
total -= min;
Or make sure prices
has at least one element:
// fill the empty array with a zero
if(prices.length == 0) prices.push(0);
// We get the lower one
var min = Math.min.apply(Math, prices);
本文标签: javascriptApplying Mathmin() to an empty list produces Infinity instead of 0Stack Overflow
版权声明:本文标题:javascript - Applying Math.min() to an empty list produces -Infinity instead of 0 - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744555483a2612446.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论