我有一個應用程序需要購買3本書(3本書:wiz [0],lorax [1]和民主人士[2])。這三本書的價格都是24.95美元,但兩本書的價格是44.90美元(合計--5美元),三本書的價格是59.85美元(合計--10美元)。但是第4本或+書應該打折到19.95美元,沒有進一步的折扣。我困在我的第三個函數「__totalWithDiscounts()」。有沒有人有任何想法如何做到這一點?我覺得它超級簡單,但我不知道該怎麼做。適用於json中的值折扣javascript
var cartJSON = [{'id':'wiz','count':0},
{'id':'gorax','count':0},
{'id':'democrat','count':0}]
function __updateTheTotal(item,quantity){
// find book being updated and change the count to the passed quantity
for (var i=0; i<cartJSON.length; i++) {
if (cartJSON[i].id == item) {
cartJSON[i].count = quantity;
break;
}
}
__totalWithDiscounts();
}
function __totalWithDiscounts(){
// this function will get the new json data generated by the inputs and apply discounts based on the amount
var discount_offTwoBooks = Number(5);
var discount_offThreeBooks = Number(10);
var priceOfEachBook_default = Number(24.95);
var priceOfEachBook_afterCountIs4 = Number(19.95);
var totalOf_wiz = Number(cartJSON[0].count);
var totalOf_gorax = Number(cartJSON[1].count);
var totalOf_democrat = Number(cartJSON[2].count);
var totalOf_all = +totalOf_wiz +totalOf_gorax +totalOf_democrat;
console.log('total books: '+totalOf_all);
}