2016-04-26 10 views
0

私は購入する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); 
} 

答えて

0

私はこれを行います。これをテストしていないが、それがうまくいくと確信している:

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 = 0+totalOf_wiz +totalOf_gorax +totalOf_democrat; 
    console.log('total books: '+totalOf_all); 

    var priceOfAll = 0; 
    if(totalOf_all >=4) priceOfAll = totalOf_all * priceOfEachBook_afterCountIs4; 
    else if(totalOf_all == 3) priceOfAll = priceOfEachBook_default * 3 - discount_offThreeBooks; 
    else if(totalOf_all == 2) priceOfAll = priceOfEachBook_default * 2 - discount_offTwoBooks; 
    else priceOfAll = priceOfEachBook_default * 1; 
    } 

アドバイスのビット:より良い変数名を使用する。そして、これは分岐の単純な論理的な構成でした。把握することは困難ではない。ここでhttps://en.wikipedia.org/wiki/Conditional_(computer_programming)

0

は溶液でjsfiddleです:https://jsfiddle.net/6p1n1tta/3/

var totalOf_all = totalOf_wiz + totalOf_gorax + totalOf_democrat; 
var discount = 0; 
    switch (totalOf_all) { 
    case 1: { 
     discount = 0; 
     break; 
    } 
    case 2: { 
     discount = discount_offTwoBooks; 
     break; 
    } 
    case 3: { 
     discount = discount_offThreeBooks; 
     break; 
    } 
    default: { 
     discount = priceOfEachBook_afterCountIs4; 
     break; 
    } 
    } 
var totalPrice = (totalOf_all * priceOfEachBook_default) - discount; 

console.log('total books: ' + totalOf_all + '\n' + 'total price: ' + totalPrice); 

また、あなたの__updateTheTotal機能では、それがcartJSON[i].count += quantity;する必要があります。変更を加えるまで、カウントは正しく更新されません。

関連する問題