对 JSON JavaScript 中的值应用折扣

apply discounts to values in json javascript

本文关键字:应用 JSON JavaScript      更新时间:2023-09-26

我有一个应用程序,里面有 3 件物品可供购买(3 本书:wiz[0]、lorax[1] 和 democrat[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);
}

我会这样做。还没有测试过这个,但很确定它会起作用:

    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)

这是一个带有解决方案的 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; . 在进行该更改之前,计数不会正确更新。