Tell us what’s happening:
Hello,
The idea was that I am counting out the bills, starting from the largest. However, on the third test, when it subtracts the second 20$ bill, a rounding error of a very small amount seems to get introduced. At the end that results in the last penny not being paid out and the test fails. I am stuck on why this might be the case. Any hints would be highly appreciated.
Your code so far
function checkCashRegister(price, cash, cid) {
var change = {
status: "OPEN",
change: []
};
let billRef = {
"PENNY": 0.01,
"NICKEL": 0.05,
"DIME": 0.1,
"QUARTER": 0.25,
"ONE": 1,
"FIVE": 5,
"TEN": 10,
"TWENTY": 20,
"ONE HUNDRED": 100
}
let amountDue = cash - price;
for (let i = cid.length - 1; i >= 0; i--) {
let currentBillVal = billRef[cid[i][0]];
let currentBillAmt = cid[i][1];
let currentBillName = cid[i][0];
if (currentBillVal <= amountDue) {
let nextBill = [currentBillName]
let toGiveFromCurrent = 0;
while (amountDue >= currentBillVal && currentBillAmt >= currentBillVal) {
console.log(amountDue)
toGiveFromCurrent = toGiveFromCurrent + currentBillVal;
currentBillAmt = currentBillAmt - currentBillVal;
amountDue = amountDue - currentBillVal;
}
nextBill.push(toGiveFromCurrent)
change.change.push(nextBill);
}
}
return 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]]
console.log(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]]));
Your browser information:
User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.13; rv:73.0) Gecko/20100101 Firefox/73.0
.
Challenge: Cash Register
Link to the challenge: