我有一个应用程序需要购买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);
}