Created
May 5, 2020 17:25
-
-
Save CEOehis/fbd86eb0b7fbb352e23c541f03abce11 to your computer and use it in GitHub Desktop.
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 characters
| const CENTS = 100; | |
| function toCents(amount) { | |
| return Math.ceil(amount * CENTS); | |
| } | |
| function toUSD(amount) { | |
| return amount / CENTS; | |
| } | |
| function checkCashRegister(price, cash, cid) { | |
| const originalDrawer = [...cid]; | |
| var change = toCents(cash - price); | |
| const billNames = { | |
| 'PENNY': 1, | |
| 'NICKEL': 5, | |
| 'DIME': 10, | |
| 'QUARTER': 25, | |
| 'ONE': 100, | |
| 'FIVE': 500, | |
| 'TEN': 1000, | |
| 'TWENTY': 2000, | |
| 'ONE HUNDRED': 10000, | |
| } | |
| var bills = 0; | |
| var changeGiven = []; | |
| for(let i = cid.length - 1; i >= 0; i--) { | |
| let [bill, amount] = cid[i]; | |
| amount = toCents(amount); | |
| while(change >= billNames[bill] && amount) { | |
| amount -= billNames[bill]; | |
| change -= billNames[bill]; | |
| bills++; | |
| } | |
| if(bills) { | |
| changeGiven.push([bill, toUSD(billNames[bill] * bills)]); | |
| bills = 0; | |
| } | |
| if(!amount) { | |
| cid[i] = null; | |
| } | |
| } | |
| const data = { | |
| status: 'INSUFFICIENT_FUNDS', | |
| change: [], | |
| } | |
| for(const quid of cid) { | |
| data.status = 'INSUFFICIENT_FUNDS'; | |
| if(quid) break; | |
| data.status = 'CLOSED'; | |
| } | |
| if(data.status == 'CLOSED' && !change) { | |
| data.change = originalDrawer; | |
| console.log(data, originalDrawer, change) | |
| return data; | |
| } | |
| if(change) { | |
| data.status = 'INSUFFICIENT_FUNDS'; | |
| data.change = []; | |
| } | |
| if(!change) { | |
| data.status = 'OPEN'; | |
| data.change = changeGiven; | |
| }; | |
| return data; | |
| } | |
| checkCashRegister(19.5, 20, [["PENNY", 0.01], ["NICKEL", 0], ["DIME", 0], ["QUARTER", 0], ["ONE", 0], ["FIVE", 0], ["TEN", 0], ["TWENTY", 0], ["ONE HUNDRED", 0]]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment