将折扣应用于json javascript中的值

时间:2016-04-26 22:44:06

标签: javascript json

我有一个应用程序有3个项目要购买(3本书:wiz [0],lorax [1]和民主党[2])。这3本书全部售价24.95美元,但两本书售价44.90美元(总共减$ 5),三本书售价59.85美元(总共减少10美元)。但是第4或第+本书的折扣应该是19.95美元,没有进一步的折扣。我被困在我的第三个功能" __ totalWithDiscounts()"。有谁知道怎么做?我觉得它非常简单,但我不知道该怎么做。

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);
}

2 个答案:

答案 0 :(得分: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)

答案 1 :(得分: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;。在您进行更改之前,计数将无法正确更新。