Javascript 兑换20%凭证后重新计算JS价格
尝试在应用折扣后重新计算JS中的最终价格,但似乎不起作用。总购物车和兑换优惠券功能刚刚好,但我不能得到最终的价格Javascript 兑换20%凭证后重新计算JS价格,javascript,price,coupon,Javascript,Price,Coupon,尝试在应用折扣后重新计算JS中的最终价格,但似乎不起作用。总购物车和兑换优惠券功能刚刚好,但我不能得到最终的价格 // TOTAL CART obj.totalCart = function () { var totalCart = 0; for (var item in cart) { totalCart += cart[item].price * cart[item].count; } return Number(totalCart.toFixed(2));
// TOTAL CART
obj.totalCart = function () {
var totalCart = 0;
for (var item in cart) {
totalCart += cart[item].price * cart[item].count;
} return Number(totalCart.toFixed(2));
}
// REDEEM COUPON
function validate(discount) {
var disc = "WE56DQ1";
var coupon = disc.trim();
var input = document.getElementById('discount').value;
if (input.toUpperCase() == coupon.toUpperCase()) {
document.getElementById('message').innerHTML = "Discount applied!";
document.getElementById('err').innerHTML = "";
return true;
} else {
document.getElementById('err').innerHTML = "Invalid discount";
document.getElementById('message').innerHTML = "";
return false;
}
}
// FINAL PRICE AFTER COUPON
obj.totalPrice = function () {
var discount = 20;
return totalPrice = totalCart - (totalCart * discount / 100);
}
要做到这一点,您只需尝试:
newprice=Math.floor((oldprice/10)*80)
或
newprice=Math.floor((旧价格/100)*(100折扣))
谢谢你花时间发帖子
但是,如果您计划将其制作成商店,最好在服务器上进行所有计算,除非js是服务器端的。请正确格式化代码,并添加错误和预期输出的示例。预期输出是应用折扣后的最终价格,根本不显示。考虑过将最终价格转换为if语句,如果应用了折扣,则
“totalPrice=Math.floor((totalCart/100)*(100折扣))
else-newprice=totalCart,但我无法理解此代码。感谢您的快速回答。考虑过将newprice转换为if-else语句条件,如果应用了折扣,则”newprice=Math.floor((oldprice/10)*80)“否则newprice=totalCart,但我无法理解此代码。谢谢)