if语句不在for循环中执行

时间:2021-12-18 20:27:11

Not sure why the if statement inside the second for loop not executing, anyone who can point me in the right direction would be appreciated.

不知道为什么第二个for循环中的if语句没有执行,任何能指出我正确方向的人都会受到赞赏。

function checkCashRegister(price, cash, cid) {
  var change;
  var balance = [];
  var total = 0;
  for (var i = 0; i<cid.length; i++) {
    total += cid[i][1];
    change = cash - price;  
    if (total < change) {
      return "Insufficient Funds";
    } else if (total === change) {
      return "Closed";
    } 
  }
  for (var x = 8; x >= 0; x--) {
    if (change > cid[x][1] && change <= cid[x][1]) {
      balance.push(cid[x-1][1]);
    }
  }
  return balance;
} //end function

checkCashRegister(19.50, 20.00, [["PENNY", 1.01], ["NICKEL", 2.05], ["DIME",     3.10], ["QUARTER", 4.25], ["ONE", 90.00], ["FIVE", 55.00], ["TEN", 20.00], ["TWENTY", 60.00], ["ONE HUNDRED", 100.00]]);

2 个解决方案

#1


1  

The if statement is trying to check if change is greater than cid[x][1] AND if change is less than/equal to cid[x][1].

if语句试图检查更改是否大于cid [x] [1]并且如果更改小于/等于cid [x] [1]。

This will never be true.

这永远不会成真。

change will always either be greater than cid[x][1] or less than/equal to it.

更改将始终大于cid [x] [1]或小于/等于它。

#2


1  

Let's look at the logic of the if statement: change > cid[x][1] && change <= cid[x][1]

让我们看一下if语句的逻辑:change> cid [x] [1] && change <= cid [x] [1]

You're saying that if change is greater than cid[x][1] AND change is less than or equal to cid[x][1]. That logic will never execute because one number can never be both larger AND smaller than or equal to another's number at the same time.

你说如果变化大于cid [x] [1]并且变化小于或等于cid [x] [1]。该逻辑永远不会执行,因为一个数字永远不能同时大于或小于或等于另一个数字。

Let change = 5 and cid[x][1] = 0.25, plugging those value into the logic you'll see that it's not possible for 5 to be larger AND smaller than or equal to 0.25 at the same time. (5 > 0.25 && 5 <= 0.25).

让change = 5和cid [x] [1] = 0.25,将这些值插入到逻辑中,您将看到5不可能同时大于和小于或等于0.25。 (5> 0.25 && 5 <= 0.25)。

#1


1  

The if statement is trying to check if change is greater than cid[x][1] AND if change is less than/equal to cid[x][1].

if语句试图检查更改是否大于cid [x] [1]并且如果更改小于/等于cid [x] [1]。

This will never be true.

这永远不会成真。

change will always either be greater than cid[x][1] or less than/equal to it.

更改将始终大于cid [x] [1]或小于/等于它。

#2


1  

Let's look at the logic of the if statement: change > cid[x][1] && change <= cid[x][1]

让我们看一下if语句的逻辑:change> cid [x] [1] && change <= cid [x] [1]

You're saying that if change is greater than cid[x][1] AND change is less than or equal to cid[x][1]. That logic will never execute because one number can never be both larger AND smaller than or equal to another's number at the same time.

你说如果变化大于cid [x] [1]并且变化小于或等于cid [x] [1]。该逻辑永远不会执行,因为一个数字永远不能同时大于或小于或等于另一个数字。

Let change = 5 and cid[x][1] = 0.25, plugging those value into the logic you'll see that it's not possible for 5 to be larger AND smaller than or equal to 0.25 at the same time. (5 > 0.25 && 5 <= 0.25).

让change = 5和cid [x] [1] = 0.25,将这些值插入到逻辑中,您将看到5不可能同时大于和小于或等于0.25。 (5> 0.25 && 5 <= 0.25)。