0
我正在使用javascript在线订单。我几乎有它的工作,但我想要做的是申请12.5%的折扣,如果超过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项,计算出来是错误的。例如,如果我有20箱苹果汁,20英镑和1箱橙色22英镑,12.5%的折扣,我应该得到总共106.75英镑,但我得到95.81英镑。
我不知道我是否犯了一个明显的错误。任何人都可以给我任何意见,我做错了什么?
您是通过每一次'0.875'乘以'juiceTotal'你添加一个贴现产品。你的数学出来了((20 * 5 * 0.875)+(22 * 1))* 0.875'。在所有价格总结后(并将其舍入到固定的小数位数),应用折扣*一次*。在你的代码中还有其他的问题,比如变量声明时没有'var','parseInt'而没有明确的基址... – DCoder 2012-07-28 16:46:59
还有'totalItems + = Number(parseInt(juiceForm [i] .value))'''juiceTotal + =(itemQuantity * juicePrice [i]))* 0.875;'就足够了 – pankar 2012-07-28 16:51:05