JavaScript 商店脚本无法正常运行

JavaScript shop script won’t function as intended

我正在尝试制作此商店脚本以包含以下条件:

  1. 余额不能低于0
  2. 当资金不足时,将使用代金券。
  3. 它记录凭证的使用和成功确认。

现在余额确实低于零,优惠券也没有被使用...也许这是显而易见的,但我被逼疯了。

此外,有什么优化此代码的技巧吗?

谢谢!如果这很明显,我很抱歉。我对此完全陌生!

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) + "$.");
}
debugger;
var total = 500;
var balance = 400;
var voucher = 100;
var balanceWithVoucher = balance + voucher;
total > balance ? useVoucher = true : useVoucher = false;


if ((total > balance && total > balanceWithVoucher) || (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) + "$.");
}

有几个错误,这里是修复版本:

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) {
    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) > 0 && balance > total) {
    console.log("Success! You have a new balance of " + (balance - total) + "$.");
}
  1. 不要在 if 语句中使用 ;。这不是必需的,并且会导致您的逻辑执行不正确。
  2. 不要执行 (useVoucher = true) 这会将值 true 赋给变量 useVoucher 而不是检查它是否为 true。改为执行 (useVoucher)。这将检查 useVoucher 的值是否不等于 false.

再次尝试检查您的逻辑。似乎只有在资金充足的情况下才会使用代金券。我认为您应该删除“else if”逻辑并在第一个“if”块中输入代金券用法。

首先你必须检查你是否可以使用代金券。如果您没有足够的余额,但使用代金券有足够的余额,就会发生这种情况。

在这种情况下使用代金券。

然后检查您是否有足够的余额支付。

代码如下:

var total = 500;
var balance = 400;
var voucher = 100;
useVoucher = true;


if (total > balance     // Not enough balance
      && useVoucher     // You can use the voucher
      && total - voucher <= balance) {  // Total minus voucher is under balance
  console.log(voucher + "$ Voucher applied. The new total is " + total + "$.");
  // IN this case use voucher
  total = total - voucher;

  // Would be better to check here if total is under 0, 
  // otherwise in the next few lines of code you will increase your balance.
} 

if (total <= balance) {   // Check if you have enough balance
  balance = balance - total;
  console.log("Success! You have a new balance of " + balance + "$.");
} else {                  // Not enough balance
  console.log("Insufficient funds. The total is " + total + "$ and you only have a balance of " + balance + "$.");
}

应进行额外检查以检查代金券是否大于 total.Generally 使用代金券,如果代金券大于价格,您将失去代金券的额外资金,但在这种情况下我没有' 添加了此检查,因为它没有被明确请求。