Bug in checking tool(?) - Cash Register

Bug in checking tool(?) - Cash Register
0.0 0

#1

Tell us what’s happening:
Hi people,

so I have created a reasonably well working cash register here. However, what strikes me as odd is the fact that I have written a simple check for too little money in the cash register. See below:

// checking for total cash
for (var coin in cid) {
totalMoney += cid[coin][1];
}

// checking for too little money
if (change > totalMoney) {
console.log(change);
console.log(totalMoney);
message.status = IF;
message.change = [];
return message;
}

This works well on the first test, but comes back negative on the second test with:
checkCashRegister(19.5, 20, [[“PENNY”, 0.01], [“NICKEL”, 0], [“DIME”, 0], [“QUARTER”, 0], [“ONE”, 1], [“FIVE”, 0], [“TEN”, 0], [“TWENTY”, 0], [“ONE HUNDRED”, 0]]) should return {status: “INSUFFICIENT_FUNDS”, change: []}.

I feel like this method is so simple, it should either work or not work. Would you be able to point me at what exactly is not right here? Thank you.

Your code so far


// listing coins and bills in decending order to start checking for highest bills
var denominations = [
  { 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}
];

function checkCashRegister(price, cash, cid) {
  var change = cash - price;
  var totalMoney = 0;
  const IF = "INSUFFICIENT_FUNDS";
  const CL = "CLOSED";
  const OP = "OPEN";
  var message = {
    status: "Default",
    change : []
  };

  // checking for total cash
  for (var coin in cid) {
    totalMoney += cid[coin][1];
  }

  // checking for too little money
  if (change > totalMoney) { 
    console.log(change);
    console.log(totalMoney);
    message.status = IF;
    message.change = [];
    return message;
  }

  // checking for exact change
  if (change == totalMoney){
    message.status = CL;
    message.change = cid;
    return message;
  }

  // creating object from array
  var drawer = {};
  for (var i = 0; i < cid.length; i++){
    drawer[cid[i][0]] = cid[i][1];
  }

  // looping through the bills/coins until all change is returned
  for (var i = 0; i < denominations.length; i++){
    var value = 0;
    while (drawer[denominations[i]["name"]] > 0 && change >= denominations[i]["value"]){
      change -= denominations[i]["value"];
      drawer[denominations[i]["name"]] -= denominations[i]["value"];
      value += denominations[i]["value"];

      change = Math.round(change * 100) / 100;
    }
    if (value > 0){
      message.change.push([denominations[i]["name"], value]);
    }
  }

  message.status = OP;
  return message;
}  


// 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", 0.01], ["NICKEL", 0], ["DIME", 0], ["QUARTER", 0], ["ONE", 0], ["FIVE", 0], ["TEN", 0], ["TWENTY", 0], ["ONE HUNDRED", 0]]);

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.87 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

Nevermind - I solved it on my own. How do I delete this?