# Cash Register - It's not working, and I don't know why

Cash Register - It's not working, and I don't know why
0

#1

Tell us what’s happening:
For the second time, I’m going to the forums to ask for help on an unexplained error
Basically, my code works down through the denominations, checking whether I can give some change from that draw.
However, for the case where I need to give 96 and a bit dollars in change, my program decides to give all of it in pennies. Why? I have no clue.
Any help will be greatly appreciated.

function checkCashRegister(price, cash, cid) {
var change;
// Here is your change, ma'am.
var currency = {
PENNY: 0.01,
NICKEL: 0.05,
DIME: 0.1,
QUARTER: 0.25,
ONE: 1,
FIVE: 5,
TEN: 10,
TWENTY: 20,
"ONE HUNDRED": 100
};
function getChange(amount, cid) {
var oldCid = Object.assign({}, cid);
var out = [];
console.log("Original amount is " + amount);
for (var i = 0; i < cid.length; i++) {
cid[i][1] /= currency[cid[i][0]];
cid[i][1] = Math.round(cid[i][1]);
}
var oldAmount;
for (var i = cid.length - 1; i >= 0; i--) {
//i = (cid.length-1)-j
//console.log("cid" + cid)
if (
Math.floor(amount / currency[cid[i][0]]) > 0 &&
Math.floor(amount / currency[cid[i][0]]) * currency[cid[i][0]] < oldCid[i][1]
) {
//console.log("Before")
oldAmount = amount;
var subtractedAmount =
Math.floor(amount / currency[cid[i][0]]) * currency[cid[i][0]];
console.log("Amount before " + amount);
console.log("Currency to be removed " + currency[cid[i][0]]);
console.log("Subtracting " + subtractedAmount);
amount -= subtractedAmount;
console.log("Amount after " + amount);
//console.log("After")
out.push([cid[i][0], subtractedAmount]);
console.log("Used " + cid[i][0] + " so now amount is " + amount);
}
else{
}
//console.log("Passed if")
//console.log("cid after if " + cid)
}
//console.log("Amount is " + amount)

if (amount <= 0) {
console.log("Returning " + out);
return out;
}
return false;
}

var total = 0;
for (var i = 0; i < cid.length; i++) {
total += cid[i][1];
}
if (cash - price > total) {
return { status: "INSUFFICIENT_FUNDS", change: [] };
}
if (cash - price === total) {
return { status: "CLOSED", change: cid };
} else {
change = getChange(cash - price, cid);
if (change) {
return { status: "OPEN", change: change };
}
return { status: "INSUFFICIENT_FUNDS", change: [] };
}
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]]
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]
]);

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

#2

I can understand why you’d try to find the number of bills, but wouldn’t you be easier off leaving the total value of the tender? That way, you can just subtract, rather than converting back and forth and possibly getting errors somewhere.

#3

I found a couple of errors, including that oldCid was not actually copied, but simply referencing the same memory, and that I wasn’t subtracting the amount if the amount couldn’t get more than the total.
Point being, I’ve gotten it working.

#4

Oh good! Sorry I couldn’t have been more helpful!

#5