【发布时间】:2018-12-11 13:14:50
【问题描述】:
我正在尝试让这个商店脚本包含以下条件:
- 余额不能低于0
- 资金不足时,将使用代金券。
- 它记录凭证的使用和成功确认。
现在余额确实低于零,代金券也没有被使用……也许这很明显,但我被逼疯了。
另外,有什么技巧可以优化这段代码吗?
谢谢!如果这非常明显,我很抱歉。我对此完全陌生!
var total = 500;
var balance = 400;
var voucher = 100;
useVoucher = true;
if ( (total > balance) || (balance < 0) ) {
console.log("Insufficient funds. The total is " + total + "$ and you only have a balance of " + balance + "$.");
} else if (useVoucher = true) {
total = total - voucher
console.log(voucher + "$ Voucher applied. The new total is " + total + "$.");
} else {
console.log("You can't afford this item");
}
if ((balance - total < 0) && balance > total); {
console.log("Success! You have a new balance of " + (balance - total) + "$.");
}
【问题讨论】:
-
应该是
useVoucher === true。目前您将true分配给useVoucher不检查useVoucher是否为true -
好点,谢谢!我不知道。但它没有用
标签: javascript confirmation shop