let denomValue = [
{ name: ‘ONE HUNDRED’, val: 100.00 },
{ name: ‘TWENTY’, val: 20.00 },
{ name: ‘TEN’, val: 10.00 },
{ name: ‘FIVE’, val: 5.00 },
{ name: ‘ONE’, val: 1.00 },
{ name: ‘QUARTER’, val: 0.25 },
{ name: ‘DIME’, val: 0.10 },
{ name: ‘NICKEL’, val: 0.05 },
{ name: ‘PENNY’, val: 0.01 }
];
let checkCashRegister = function (price, cash, cid) {
let change = cash - price
// transform cid into one object (number) to work with
let cashDrawer = cid.reduce(function(acc,curr){
acc.total += curr[1];
acc[curr[0]] = curr[1];
return acc;
}, { total: 0 });
// Check for cash first
// made status equal three different things depending on the if
if (cashDrawer.total < change) {
status = 'INSUFFICIENT_FUNDS'
} else if (cashDrawer.total === change) {
status = 'CLOSED'
} else {
status = 'OPEN'
}
// make a loop to go through the denom and add value from Greatest to smallest.
let result = denomValue.reduce(function (acc, curr) {
let value = 0
while (cashDrawer[curr.name] > 0 && change >= curr.val) {
change -= curr.val
cashDrawer[curr.name] -= curr.val
value += curr.val
// math round to deal with run off numbers.
change = Math.round(change * 100) / 100
}
// this if statement is basically saying if we added to value print down what we added. with a push statement
if (value > 0) {
acc.push(['["' + curr.name + '"', value.toFixed(2) + ']'])
}
return acc
}, []);
// returned all into a simple template string
{status: "${status}", change: [${result}]}
}
checkCashRegister(2.50, 10, [[“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.5], [“NICKEL”, 0], [“DIME”, 0], [“QUARTER”, 0], [“ONE”, 0], [“FIVE”, 0], [“TEN”, 0], [“TWENTY”, 0], [“ONE HUNDRED”, 0]])