我正在使用 javascript 处理在线订单。我几乎可以使用它,但是如果选择了 5 个以上的商品,我想做的是应用 12.5% 的折扣。到目前为止,如果选择了多个项目,我已经设法获得折扣。这是我的代码:
var totalItems = 0
// Run through the form fields to check for any filled fields
for (var i=0; i<juiceForm.length; i++)
{
n=0;
juicetotal = 0;
itemQuantity = Number(parseInt(juiceForm[i].value)); // convert field value to a number
itemQuantity = parseInt(juiceForm[i].value);
if (isNaN(itemQuantity))
{
itemQuantity = 0; // If the form field value is not a number, make it zero
}
// count the total number of juices selected
totalItems = totalItems += Number(parseInt(juiceForm[i].value));
if (totalItems >= 5 || itemQuantity >= 5 || (totalItems + itemQuantity) >= 5)
{
juiceTotal = (juiceTotal+(itemQuantity * juicePrice[i]))*0.875;
}
else
{
// Multiply the quantity by the item price and update the order total
juiceTotal = juiceTotal+(itemQuantity * juicePrice[i]);
}
}
我遇到麻烦的地方是,如果选择了多个项目总共超过 5 个项目,那么计算就会出错。例如,如果我有 5 箱 20 英镑的苹果汁和 1 箱 22 英镑的橙子,加上 12.5% 的折扣,我总共应该得到 106.75 英镑,但我得到的是 95.81 英镑。
我不确定我是否犯了明显的错误。谁能给我任何关于我做错了什么的建议?