Created
October 15, 2019 03:27
-
-
Save tianmingzuo/de2270d37c9de787f43d4d7fc7452a00 to your computer and use it in GitHub Desktop.
Cash Register: Design a cash register drawer function checkCashRegister() that accepts purchase price as the first argument (price), payment as the second argument (cash), and cash-in-drawer (cid) as the third argument. cid is a 2D array listing available currency. The checkCashRegister()function should always return an object with a status key …
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
| function checkCashRegister(price, cash, cid) { | |
| let billArr = [["ONE HUNDRED", 100], ["TWENTY", 20], ["TEN", 10], ["FIVE", 5], | |
| ["ONE", 1], ["QUARTER", 0.25], ["DIME", 0.1], ["NICKEL", 0.05], | |
| ["PENNY", 0.01]]; | |
| let changeDue = cash - price; | |
| let sumCash = 0; | |
| for(let i=0; i<cid.length; i++){ | |
| sumCash += cid[i][1]; | |
| } | |
| if(sumCash < changeDue){ | |
| return {status: "INSUFFICIENT_FUNDS", change: []}; | |
| } | |
| if(sumCash === changeDue){ | |
| return {status: "CLOSED", change: cid}; | |
| } | |
| let revCid = cid.reverse(); //reverse() method changes the original array(cid) | |
| let changeArr = []; | |
| for(let i=0; i<revCid.length; i++){ | |
| let count = 0; | |
| while(revCid[i][1] > 0 && changeDue >= billArr[i][1]){ | |
| revCid[i][1] -= billArr[i][1]; | |
| changeDue -= billArr[i][1]; | |
| changeDue = Math.round(changeDue*100)/100; | |
| count++; | |
| } | |
| if(count > 0){ | |
| changeArr.push([billArr[i][0], billArr[i][1]*count]); | |
| } | |
| } | |
| if(changeDue === 0){ | |
| return {status: "OPEN", change: changeArr}; | |
| } | |
| //here, although sumCash is enough, but you cannot return the exact change. | |
| return {status: "INSUFFICIENT_FUNDS", change: []}; | |
| } | |
| // Example cash-in-drawer array: | |
| // [["PENNY", 1.01], | |
| // ["NICKEL", 2.05], | |
| // ["DIME", 3.1], | |
| // ["QUARTER", 4.25], | |
| // ["ONE", 90], | |
| // ["FIVE", 55], | |
| // ["TEN", 20], | |
| // ["TWENTY", 60], | |
| // ["ONE HUNDRED", 100]] | |
| //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]]); | |
| //console.log(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]]).status); | |
| 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]]); | |
| //checkCashRegister(19.5, 20, [["PENNY", 0.5], ["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