Currently my checkCashRegister function outputs:
{ status: 'OPEN',
change: [ [ 'TWENTY', 20 ], [ 'TWENTY', 20 ], [ 'TWENTY', 20 ], [ 'TWENTY', 20 ],[ 'TEN', 10 ],
[ 'FIVE', 5 ], [ 'ONE', 1 ],[ 'QUARTER', 0.25 ],[ 'QUARTER', 0.25 ], [ 'DIME', 0.1 ], [ 'DIME', 0.1 ],
[ 'PENNY', 0.01 ],[ 'PENNY', 0.01 ],[ 'PENNY', 0.01 ],[ 'PENNY', 0.01 ] ] }
which does equate to the correct change needed.
HOWEVER, I need to decrement each cash-in-drawer as well.
This line, does output the correct new value for each cash-in-drawer. But I canโt figure out how or where to include it in my getChangeCoins() function. Where does it fit in the loop?
console.log(cashInDrawer[d][1] -= coinsObj[cashInDrawer[d][0]]); //0 40 10 50 89 4 3 1.9999999999999998 1
function checkCashRegister(price, cash, cid) {
let totalChange = cash - price; //96.74
let totalCID = 0;
let cashInDrawer = cid.reverse(); // sorted highest to lowest
let change = [];
let coinsObj = {"ONE HUNDRED":100, "TWENTY":20, "TEN":10, "FIVE":5, "ONE":1, "QUARTER":0.25, "DIME":0.1, "NICKEL":0.05, "PENNY":0.01};
let coinsArray = [["ONE HUNDRED",100], ["TWENTY",20], ["TEN",10], ["FIVE",5], ["ONE",1], ["QUARTER",0.25], ["DIME",0.1], ["NICKEL",0.05], ["PENNY",0.01]];
for (let i=0; i<cid.length;i++) {
totalCID += cid[i][1]; //total up the cash-in-drawer
}
if (totalCID < totalChange) {
change = getChangeCoins(totalChange);
return {'status' : "INSUFFICIENT_FUNDS", 'change' : []}
} else if (totalCID == totalChange) {
change = getChangeCoins(totalChange);
return {'status' : "CLOSED", 'change' : []}
} else {
change = getChangeCoins(parseFloat(totalChange.toFixed(2)));
return {'status' : "OPEN", 'change' : change}
}
function getChangeCoins(changeVal) {
for (let c=0; c<coinsArray.length; c++) {
for (let d=0; d<=cashInDrawer.length; d++) {
console.log(cashInDrawer[d][1] -= coinsObj[cashInDrawer[d][0]]); //0 40 10 50 89 4 3 1.9999999999999998 1
while (coinsArray[c][1] <= parseFloat(changeVal.toFixed(2))) {
changeVal -= coinsArray[c][1];
change.push(coinsArray[c]);
}
}
}
return change;
}
}
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]])
// {status: "OPEN", change: [["TWENTY", 60], ["TEN", 20], ["FIVE", 15], ["ONE", 1], ["QUARTER", 0.5], ["DIME", 0.2], ["PENNY", 0.04]]}