Need help with the cash register challenge

Hi!

It returns what it is expected but still not being approved by the checker… Could anybody show me where is my mistake?

function checkCashRegister(price, cash, cid) {
  var toReturn = cash - price;
  var cashArray = [0.01, 0.05, 0.1, 0.25, 1, 5, 10, 20, 100];
  var counter = 0;
  var total;
  var result = {
    status: "",
    change: [["ONE HUNDRED", 0], ["TWENTY", 0], ["TEN", 0], ["FIVE", 0], ["DOLLAR", 0], ["QUARTER", 0], ["DIME", 0], ["NICKEL", 0], ["PENNY", 0]]
  }

  total = cid.reduce(function(acc,val){
    return (acc * 100 + val[1] * 100) / 100;
  },0);

  if(total < toReturn){
    result.status = "INSUFFICIENT_FUNDS";
    result.change = [];
    return result;
  } else if(total == toReturn){
    result.status = "CLOSED";
    result.change = cid;
    return result;
  } else {
    result.status = "OPEN";
  for(var i = cid.length - 1; i >= 0; i--){
      while(cid[i][1] > 0 && toReturn - cashArray[i] >= 0){
        cid[i][1] = cid[i][1] - cashArray[i];
        result.change[counter][1] += cashArray[i];
        toReturn = toReturn - cashArray[i];
        var b = Math.round(toReturn * 100);
        toReturn = b / 100;
      }

    counter++
  }
  }

  if (toReturn != 0){
    result.status = "INSUFFICIENT_FUNDS";
    result.change = [];
    return result;
  }
  var filtered = result.change.filter(function(x){
    return x[1] > 0;
  });
  result.change = filtered;
  return result;
}


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]]);

It is failing test #3

https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/javascript-algorithms-and-data-structures-projects/cash-register/

test #3:

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

1 Like

For the purposes of this test, “What’s a ‘DOLLAR’”?

1 Like

I can’t believe that mistake! I was copying the list provided and thought that “DOLLAR” was the “ONE”, I didn’t notice that it was “ONE” on the rest. Thank you!

1 Like