The log statement prints out every one I need except the pennies.
const currencyChart = {
'Penny': 1,
'NICKEL': 5,
'DIME': 1,
'QUARTER': 25,
'ONE': 100,
'FIVE': 500,
'TEN': 1000,
'TWENTY': 2000,
'ONE HUNDRED': 10000
}
function checkCashRegister(price, cash, cid) {
// multiply all values by 100 bc it is easier to work with all integers
let changeSum = cash * 100 - price * 100;
// storing a copy bc I need to make modifications to it below
let changeSumCheck = changeSum;
let change = [];
let status = '';
let cidSum = 0;
let filteredCid = cid.filter(elem => elem[1] !== 0).reverse();
filteredCid.forEach(elem => {
let curr = elem[0];
let currSum = elem[1] * 100;
cidSum += currSum;
let amount = 0;
while(changeSum >= currencyChart[curr] && currSum > 0) {
amount += currencyChart[curr];
changeSum -= currencyChart[curr];
currSum -= currencyChart[curr];
}
if(amount !== 0) {
change.push([curr, amount / 100]);
}
});
if(changeSum > 0) {
status = 'INSUFFICIENT_FUNDS';
change = [];
}
else if (changeSum == 0 && changeSumCheck == cidSum) {
status = 'CLOSED';
change = cid;
}
else {
status = 'OPEN';
}
console.log({'status': status, 'change': change})
return {'status': status, 'change': 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]]);