Created
May 30, 2021 14:31
-
-
Save sakilk130/2d824ebbd4db0fa8220f91dcfd5733f1 to your computer and use it in GitHub Desktop.
Revisions
-
sakilk130 revised this gist
May 30, 2021 . No changes.There are no files selected for viewing
-
sakilk130 created this gist
May 30, 2021 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,75 @@ var denom = [ { name: 'ONE HUNDRED', val: 100.0 }, { name: 'TWENTY', val: 20.0 }, { name: 'TEN', val: 10.0 }, { name: 'FIVE', val: 5.0 }, { name: 'ONE', val: 1.0 }, { name: 'QUARTER', val: 0.25 }, { name: 'DIME', val: 0.1 }, { name: 'NICKEL', val: 0.05 }, { name: 'PENNY', val: 0.01 }, ]; function checkCashRegister(price, cash, cid) { var output = { status: null, change: [] }; var change = cash - price; var register = cid.reduce( function (acc, curr) { acc.total += curr[1]; acc[curr[0]] = curr[1]; return acc; }, { total: 0 } ); if (register.total === change) { output.status = 'CLOSED'; output.change = cid; return output; } if (register.total < change) { output.status = 'INSUFFICIENT_FUNDS'; return output; } var change_arr = denom.reduce(function (acc, curr) { var value = 0; while (register[curr.name] > 0 && change >= curr.val) { change -= curr.val; register[curr.name] -= curr.val; value += curr.val; change = Math.round(change * 100) / 100; } if (value > 0) { acc.push([curr.name, value]); } return acc; // Return the current change_arr }, []); if (change_arr.length < 1 || change > 0) { output.status = 'INSUFFICIENT_FUNDS'; return output; } output.status = 'OPEN'; output.change = change_arr; return output; } checkCashRegister(19.5, 20, [ ['PENNY', 1.01], ['NICKEL', 2.05], ['DIME', 3.1], ['QUARTER', 4.25], ['ONE', 90], ['FIVE', 55], ['TEN', 20], ['TWENTY', 60], ['ONE HUNDRED', 100], ]);