【问题标题】:JavaScript shop script won’t function as intendedJavaScript 商店脚本无法按预期运行
【发布时间】:2018-12-11 13:14:50
【问题描述】:

我正在尝试让这个商店脚本包含以下条件:

  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) + "$.");
}

【问题讨论】:

  • 应该是useVoucher === true。目前您将true 分配给useVoucher 不检查useVoucher 是否为true
  • 好点,谢谢!我不知道。但它没有用

标签: javascript confirmation shop


【解决方案1】:

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

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

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

代码如下:

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 + "$.");
}

应进行额外检查以检查代金券是否大于总数。通常使用代金券如果大于价格,您会损失代金券的额外金额,但在这种情况下我没有添加这个检查,因为它没有明确要求。

【讨论】:

  • 太棒了!非常感谢您的宝贵时间和帮助!
  • @Keyan 尽量以最简单的步骤拆分您的问题,而不是将这些步骤转换为代码行。这是一条简单的规则(divide et impera)
  • 知道了。您编写的代码绝对可以帮助我理解您的意思。谢谢你的提示!
【解决方案2】:

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

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

【讨论】:

  • 非常感谢!这使它不再低于零!我也很欣赏你的解释!我现在对它有了更好的理解。不过有一件事:你能用“If (balance - total > 0) > 0 &&......”来解释你上次的编辑吗?如果 balance-total 大于 0,且大于 0 大于零?谢谢!
【解决方案3】:

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

【讨论】:

  • 做到了!非常感谢您的精彩回答!不过,您将如何将其合并到第一个 IF 语句中?
  • 从“else if”中取出逻辑并将其粘贴到那里。
  • 抱歉,因为我正在通过电话接听,所以无法显示 sn-p。
  • 帮我们一个忙,选择一个答案keyan。谢谢。
【解决方案4】:
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) + "$.");
}

【讨论】:

    猜你喜欢
    • 2021-11-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-16
    • 1970-01-01
    相关资源
    最近更新 更多