我正在爲uni項目創建購物車,但在測試數組時遇到問題。如果選擇超過5個元素,總價應該有10%的折扣。如果數量大於5的數量選擇了一個元素,則變量折扣工作正常,但如果數量在兩個元素之間分配,則返回0(意思是如果POTATOS = 6,則折扣起作用但是如果POTATOS = 4且BLUEBERRY = 2它不再起作用)。 下面的代碼:嘗試在數組中使用IF語句時遇到問題
function Calc() {
var numberPotatos = document.getElementById('potatos_id').value;
var numberBlueberry = document.getElementById('blueberry_id').value;
var numberStrawberry = document.getElementById('strawberry_id').value;
var numberEggplants = document.getElementById('eggplants_id').value;
var numberKiwis = document.getElementById('kiwis_id').value;
var numberBananas = document.getElementById('bananas_id').value;
var output = "";
var MyArray= new Array (6);
MyArray [0] = numberPotatos;
MyArray [1] = numberBlueberry;
MyArray [2] = numberStrawberry;
MyArray [3] = numberEggplants;
MyArray [4] = numberKiwis;
MyArray [5] = numberBananas;
var MySecondArray = new Array (6);
MySecondArray [0] = 6;
MySecondArray [1] = 7;
MySecondArray [2] = 5;
MySecondArray [3] = 22;
MySecondArray [4] = 12;
MySecondArray [5] = 4;
// MORE CODE
var sum = 0;
for(var i=0; i< MyArray.length; i++) {
sum += MyArray [i ]* MySecondArray[i];
document.getElementById('test').innerHTML = sum;
}
var discount = 0;
for(var i=0; i<MyArray.length;i++)
{
if(MyArray[i] > 5){
discount = (sum - (sum * 0.10));
discount = parseFloat(discount);
}
document.getElementById('test2').innerHTML = discount;
}
}
閱讀您自己的問題。你真的期望有人知道你的問題是什麼嗎? – Marcin
@Marcin這對我來說非常有意義。當我們說話的時候擺弄JS。 –
@DannyBeckett「它不起作用」。顯然這是有道理的,因爲軟件只有一種失敗模式。 – Marcin