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