//value of currency denominations
var currencyTable = [100, 20, 10, 5, 1, 0.25, 0.1, 0.05, 0.01];
//total cash at each currencyTable denomination in the cash register
var cashInDrawer = [100, 60, 20, 55, 90, 4.25, 3.1, 2.05, 1.01];
//total change due
var changeDue = 96.74;
My goal is to return the correct change from the cash available from the greatest cash denomination to the lowest.
I can't figure out how to increment a value in currencyTable up to the available amount in cashInDrawer while changeDue >= currencyTable[i].
I've managed so far:
var total = [];
for (let i = 0; i < currencyTable.length; i++){
while (changeDue >= currencyTable[i]){
total.push(currencyTable[i])
changeDue-=currencyTable[i];
}
}return total;
but this only returns:
[20, 20, 20, 20, 10, 5, 1, 0.25, 0.25, 0.1, 0.1, 0.01, 0.01, 0.01]
where I want to return:
[60, 20, 15, 1, 0.5, 0.2, 0.04]
Somewhere in the loop I realize changeDue is being changed into a repeating decimal but I found a temporary workaround using .toFixed().