Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function checkCashRegister(price, cash, cid) {
- let change = cash * 100 - price * 100;
- let ncid = cid.map((x) => Number(Math.round(x[1] * 100)));
- let returnObject = { status: null, change: [] };
- const denominations = [
- ['PENNY', 1],
- ['NICKEL', 5],
- ['DIME', 10],
- ['QUARTER', 25],
- ['ONE', 100],
- ['FIVE', 500],
- ['TEN', 1000],
- ['TWENTY', 2000],
- ['ONE HUNDRED', 10000],
- ];
- for (let i = denominations.length - 1; i >= 0; i--) {
- returnObject.change.unshift([denominations[i][0], 0]);
- if (change < denominations[i][1]) {
- continue;
- } else if (change >= denominations[i][1]) {
- while (change >= denominations[i][1]) {
- if (ncid[i] - denominations[i][1] < 0) {
- console.log(ncid[i]);
- break;
- }
- change -= denominations[i][1];
- ncid[i] -= denominations[i][1];
- returnObject.change[0][1] += denominations[i][1];
- console.log(change);
- }
- }
- }
- console.log(change);
- console.log(ncid);
- returnObject.change = returnObject.change
- .map((x) => [x[0], x[1] / 100])
- .sort((a, b) => b[1] - a[1]);
- if (change > 0) {
- returnObject.status = 'INSUFFICIENT_FUNDS';
- returnObject.change = [];
- } else if (ncid.reduce((a, b) => a + b) === 0) {
- returnObject.status = 'CLOSED';
- } else {
- returnObject.status = 'OPEN';
- returnObject.change = returnObject.change.filter((x) => x[1] !== 0);
- }
- return returnObject;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement