var cashes = [
['PENNY', 1],
['NICKEL', 5],
['DIME', 10],
['QUARTER', 25],
['ONE', 100],
['FIVE', 500],
['TEN', 1000],
['TWENTY', 2000],
['ONE HUNDRED', 10000]
];
function checkCashRegister(price, cash, cid) {
var change = [];
var changeAmount = Math.round(cash * 100) - Math.round(price * 100);
var closed = true;
for (var position = cid.length - 1; position >= 0; position--) {
var changePosition = Math.floor(changeAmount / cashes[position][1]) * cashes[position][1];
var cidPosition = Math.round(cid[position][1] * 100);
if (changePosition > cidPosition) {
changePosition = cidPosition;
}
if (changePosition > 0) {
change.push([cid[position][0], changePosition / 100]);
}
changeAmount -= changePosition;
closed &= (changePosition == cidPosition);
}
if (changeAmount > 0) {
return {status: 'INSUFFICIENT_FUNDS', change: []};
}
if (closed) {
return {status: 'CLOSED', change: cid};
}
return {status: 'OPEN', change: change};
}