Cash Register - Correct Output Still Failing

Cash Register - Correct Output Still Failing
0

#1

Tell us what’s happening:
I’m getting the correct output with my code but tests 2 and 3 are still failing the checks. I am at a loss as to what I’m missing. It looks like I have the correct output format and the types match but it still fails. Any insight would be appreciated.

Your code so far


var money = [
  {name: "ONE HUNDRED", value: 100.00},
  {name: "TWENTY", value: 20.00},
  {name: "TEN", value: 10.00},
  {name: "FIVE", value: 5.00},
  {name: "ONE", value: 1.00},
  {name: "QUARTER", value: 0.25},
  {name: "DIME", value: 0.10},
  {name: "NICKEL", value: 0.05},
  {name: "PENNY", value: 0.01}
]

var count = {
  "ONE HUNDRED": 0,
  "TWENTY": 0,
  "TEN": 0,
  "FIVE": 0,
  "ONE": 0,
  "QUARTER": 0,
  "DIME": 0,
  "NICKEL": 0,
  "PENNY": 0
}

function checkCashRegister(price, cash, cid) {
  //build object of cash in drawer
  var cidObj = {};
  for (let i=0; i<cid.length; i++) {
    cidObj[cid[i][0]] = cid[i][1];
  }
  var output = {status: null, change: []}
  var change = cash - price;
  var totalCash = 0;
  for (let i=0; i<cid.length; i++) {
    totalCash += cid[i][1];
  }

  //console.log("Change due:", change);
  //console.log("Total Cash:", totalCash);

  //if total cash is less than or equal to change due
  if (totalCash === change) {
    output.status = "CLOSED";
    output.change = cid;
  } else if (totalCash < change) {
    output.status = "INSUFFICIENT_FUNDS";
  } else {
    output.status = "OPEN";
    var countSum = 0;
    for (let i=0; i<money.length; i++) {
      while (money[i].value <= change && cidObj[money[i].name] > 0 && change > 0) {
        count[money[i].name] += money[i].value;
        cidObj[money[i].name] -= money[i].value;
        change -= money[i].value;
        countSum += money[i].value;
        change = Math.round(change * 100) / 100;//change.toFixed(2)
      }
    }

    for (let i in count) {
      if (count[i] != 0) {
        output.change.push([i, count[i]]);
      }
    }

    if (countSum < change) {
      output.status = "INSUFFICIENT_FUNDS";
      output.change = [];
    }

  }
  
  // Here is your change, ma'am.
  return output;
  
}

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:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/javascript-algorithms-and-data-structures-projects/cash-register


#2

It is your use of count as a global variable which is the issue. It does not get reset to it’s initial value after each call to the checkCashRegister function, because the test suite makes consecutive calls to it, which ends up changing count between each call.


#3

Omg thank you so much! I didn’t even think about this but it makes complete sense.