私は購入する3アイテム(3冊の本:wiz [0]、lorax [1]、democrat [2])のアプリを持っています。これらの3冊はすべて24.95ドルですが、2冊は$ 44.90( - $ 5オフ)、3冊は$ 59.85($ 10オフ)です。しかし、第4回または+書籍は、さらに割引なしで19.95ドルに割り引かれるべきです。私は3番目の関数 "__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);
}