对 JSON JavaScript 中的值应用折扣
apply discounts to values in json javascript
我有一个应用程序,里面有 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;
. 在进行该更改之前,计数不会正确更新。
相关文章:
- 如何在dojo应用程序构建概要文件中加载json文件
- 如何在Chrome中找到JSON后将其放入我的应用程序's开发人员工具
- 在ColdFusion应用程序中使用JSON
- 将JSON存储和恢复到此Ionic应用程序的最有效方法
- 如何使用AngularJS应用基于Json字符串响应的条件CSS类
- 如何在angularjs应用程序中解析Drupal JSON数据
- 从web浏览器通过javascript向侦听TCP端口5500的本地应用程序发送json命令
- 如何在phonegap应用程序中处理Ajax json响应
- 如何将ember应用程序集成到json服务器中使用
- 如何在核心php应用程序中使用节点js json Web令牌库
- 在谷歌应用引擎上用jQuery处理json表单
- 将json数据存储在sqlite(phonegap应用程序)中
- (临时)在Web应用程序中存储JSON搜索结果
- 在AngularJS应用程序中加载JSON(加载谷歌电子表格)
- 对 JSON JavaScript 中的值应用折扣
- Json在应用程序电话间隙!路径是什么
- 将 json 数据加载到 CycleJS 应用程序中
- 为什么应用JSON.stringify会占用这么多内存
- 如何从外部站点访问和应用JSON数据
- Jquery Ajax发送contentType应用/json和java获取null