This while loop essentially finds what's the best unit of the currency to give back to the customer considering what is in the drawer and adds it to the change array.
while (difference > 0) {
var unit = Object.keys(currency).reverse().reduce((output,prop)=>{
if (difference >= currency[prop] && cid[prop] >= currency[prop]) {
output.push(prop, currency[prop]);
} else {}
return output;
}, []);
cid[unit[0]] -= unit[1];
difference -= unit[1];
change.push(unit);
}
- cid is cash in drawer
- List item
difference is prince minus cash
I could obviously cheat but a better solution would be preferable.