2016-04-26 76 views
0

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

回答

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;。在进行更改之前,计数无法正确更新。