0

JavaScriptを使用してオンライン注文フォームに取り組んでいます。ほぼ動いていますが、5点以上の場合は12.5%OFFにしたいです。これまでのところ、複数のアイテムが選択されている場合に割引を適用することができました. これが私のコードです:

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 ポンドのリンゴ ジュースが 5 箱、22 ポンドのオレンジ ジュースが 1 箱ある場合、12.5% の割引で合計 106.75 ポンドになるはずですが、95.81 ポンドになります。

明らかな間違いを犯したかどうかはわかりません。私が間違っていることについて誰かアドバイスをもらえますか?

4

1 に答える 1

1

多分あなたはこれについて考えます(テストされていません、擬似コードとしてそれを取ります)

var totalItems = 0
var juiceTotal = 0;
for (var i=0; i<juiceForm.length; i++)
{
    var itemQuantity = parseInt(juiceForm[i].value);
    if (isNaN(itemQuantity)) 
    {
        itemQuantity = 0;
    }
    totalItems += itemQuantity;
    juiceTotal += (juicePrice[i]*itemQuantity);  
}

if (totalItems >= 5)
    juiceTotal *= 0.875;
}
于 2012-07-28T17:03:30.520 に答える