In Cash Register problem ,the 5th test case is wrong?

Tell us what’s happening:

Your code so far

function checkCashRegister(price, cash, cid) {
  var change;
    change = cash-price;
    var len = cid.length,i,count=0,a=0;
      count = cid[i][1];
      if(i==0){count = count*0.01}
      else if(i==1){count = count*0.05}
      else if(i==2){count = count*0.1}
      else if(i==3){count = count*0.25}
      else if(i==4){count = count*1}
      else if(i==5){count = count*5}
      else if(i==6){count = count*10}
      else if(i==7){count = count*20}
      else {count = count*100}
      a = count+a; 
  if((a -change)<0){
    return {status:"INSUFFICIENT_FUNDS",change:[]}

// Example cash-in-drawer array:
// [["PENNY", 1.01],
// ["NICKEL", 2.05],
// ["DIME", 3.1],
// ["QUARTER", 4.25],
// ["ONE", 90],
// ["FIVE", 55],
// ["TEN", 20],
// ["TWENTY", 60],
// ["ONE HUNDRED", 100]]

checkCashRegister(19.5, 20, [["PENNY", 1.01], ["NICKEL", 2.05], ["DIME", 3.1], ["QUARTER", 4.25], ["ONE", 90], ["FIVE", 55], ["TEN", 20], ["TWENTY", 60], ["ONE HUNDRED", 100]]);

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36.

Link to the challenge:

The reason that test case 5 on the problem is failing is related to this criteria in the problem:

"Return {status: "INSUFFICIENT_FUNDS", change: []} if cash-in-drawer is less than the change due, or if you cannot return the exact change. "

Firstly, it looks like you are multiplying each denomination from cid by its value. If you are trying to get the total money in cash in drawer, you only need to sum the values.

then use the logic

let answer = {status: "", change: []};
  if (change > a) {
    answer.status = "INSUFFICIENT_FUNDS";
    return answer;

If you were looking to get the quantity of each denomination with your loop, you would need to divide each instance in cid by its value and store it somewhere for later (I recommend making a drawer object). You will need this to solve the 5th test case.

Remember, you need logic to determine not only if there is enough money, but if you can give the exact change with the money in your cid

1 Like