8000 [ADD] Cash Register added · luisprooc/js-algorithms@434d27c · GitHub
[go: up one dir, main page]

Skip to content

Commit 434d27c

Browse files
committed
[ADD] Cash Register added
1 parent 4b5fae9 commit 434d27c

File tree

2 files changed

+136
-0
lines changed

2 files changed

+136
-0
lines changed

README.md

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -872,4 +872,46 @@ steamrollArray([1, {}, [3, [[4]]]]) should return [1, {}, 3, 4].
872872

873873
Your solution should not use the Array.prototype.flat() or Array.prototype.flatMap() methods.
874874

875+
```
876+
877+
878+
## Cash Register
879+
880+
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.
881+
882+
cid is a 2D array listing available currency.
883+
884+
The checkCashRegister() function should always return an object with a status key and a change key.
885+
886+
Return {status: "INSUFFICIENT_FUNDS", change: []} if cash-in-drawer is less than the change due, or if you cannot return the exact change.
887+
888+
Return {status: "CLOSED", change: [...]} with cash-in-drawer as the value for the key change if it is equal to the change due.
889+
890+
Otherwise, return {status: "OPEN", change: [...]}, with the change due in coins and bills, sorted in highest to lowest order, as the value of the change key.
891+
892+
| Currency Unit | Amount |
893+
| :--- | :----:
894+
| Penny | $0.01
895+
| Nickel | $0.05
896+
| Dime | $0.1
897+
| Quarter | $0.25
898+
| Dollar | $1
899+
| Five | $5
900+
| Ten | $10
901+
| Twenty | $20
902+
| One-hundred | $100
903+
904+
```javascript
905+
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]]) should return an object.
906+
907+
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]]) should return {status: "OPEN", change: [["QUARTER", 0.5]]}.
908+
909+
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]]) should return {status: "OPEN", change: [["TWENTY", 60], ["TEN", 20], ["FIVE", 15], ["ONE", 1], ["QUARTER", 0.5], ["DIME", 0.2], ["PENNY", 0.04]]}.
910+
911+
checkCashRegister(19.5, 20, [["PENNY", 0.01], ["NICKEL", 0], ["DIME", 0], ["QUARTER", 0], ["ONE", 0], ["FIVE", 0], ["TEN", 0], ["TWENTY", 0], ["ONE HUNDRED", 0]]) should return {status: "INSUFFICIENT_FUNDS", change: []}.
912+
913+
checkCashRegister(19.5, 20, [["PENNY", 0.01], ["NICKEL", 0], ["DIME", 0], ["QUARTER", 0], ["ONE", 1], ["FIVE", 0], ["TEN", 0], ["TWENTY", 0], ["ONE HUNDRED", 0]]) should return {status: "INSUFFICIENT_FUNDS", change: []}.
914+
915+
checkCashRegister(19.5, 20, [["PENNY", 0.5], ["NICKEL", 0], ["DIME", 0], ["QUARTER", 0], ["ONE", 0], ["FIVE", 0], ["TEN", 0], ["TWENTY", 0], ["ONE HUNDRED", 0]]) should return {status: "CLOSED", change: [["PENNY", 0.5], ["NICKEL", 0], ["DIME", 0], ["QUARTER", 0], ["ONE", 0], ["FIVE", 0], ["TEN", 0], ["TWENTY", 0], ["ONE HUNDRED", 0]]}
916+
875917
```

src/cashRegister.js

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
function checkCid(cid,key){
2+
for(let i in cid){
3+
if(cid[i][0] === key){
4+
return cid[i];
5+
}
6+
}
7+
}
8+
9+
function help(cid,key,rest,value,change){
10+
const actualCid = checkCid(cid,key);
11+
let increment = Number();
12+
13+
while(rest >= 0 && actualCid[1] >= value && rest >= value){
14+
increment += value;
15+
actualCid[1] -= value;
16+
rest -= value;
17+
}
18+
19+
if(key === "PENNY"){
20+
increment += 0.01;
21+
increment = increment.toFixed(2) * 1
22+
}
23+
24+
change.push([key,increment]);
25+
return [cid,rest,change];
26+
}
27+
28+
function closeRegister(cid,change){
29+
for(let i in change){
30+
for(let j in cid){
31+
if(change[i][0] === cid[j][0]){
32+
delete cid[j];
33+
}
34+
}
35+
}
36+
return cid;
37+
}
38+
39+
function checkCashRegister(price, cash, cid) {
40+
let rest = cash - price;
41+
let change = [];
42+
while(rest >= 0.01){
43+
if(rest >= 100 && checkCid(cid,"ONE HUNDRED")[1]){
44+
[cid,rest,change] = help(cid,"ONE HUNDRED",rest,100,change);
45+
}
46+
else if(rest >= 20 && checkCid(cid,"TWENTY")[1]){
47+
[cid,rest,change] = help(cid,"TWENTY",rest,20,change);
48+
}
49+
50+
else if(rest >= 10 && checkCid(cid,"TEN")[1]){
51+
[cid,rest,change] = help(cid,"TEN",rest,10,change);
52+
}
53+
54+
else if(rest >= 5 && checkCid(cid,"FIVE")[1]){
55+
[cid,rest,change] = help(cid,"FIVE",rest,5,change);
56+
}
57+
58+
else if(rest >= 1 && checkCid(cid,"ONE")[1]){
59+
[cid,rest,change] = help(cid,"ONE",rest,1,change);
60+
}
61+
62+
else if(rest >= 0.25 && checkCid(cid,"QUARTER")[1]){
63+
[cid,rest,change] = help(cid,"QUARTER",rest,0.25,change);
64+
}
65+
66+
else if(rest >= 0.1 && checkCid(cid,"DIME")[1]){
67+
[cid,rest,change] = help(cid,"DIME",rest,0.1,change);
68+
}
69+
70+
else if(rest >= 0.05 && checkCid(cid,"NICKEL")[1]){
71+
[cid,rest,change] = help(cid,"NICKEL",rest,0.05,change);
72+
}
73+
74+
else if(rest >= 0.01 && checkCid(cid,"PENNY")[1]){
75+
[cid,rest,change] = help(cid,"PENNY",rest,0.01,change);
76+
}
77+
78+
else{
79+
return {status: "INSUFFICIENT_FUNDS", change: []}
80+
}
81+
}
82+
const status = cid.some(item => item[1] >= 0.01);
83+
if(status){
84+
return {status: "OPEN", change: change}
85+
}
86+
87+
cid = closeRegister(cid,change);
88+
change.push(...cid);
89+
change = change.filter(item => item != undefined)
90+
return {status: "CLOSED", change: change}
91+
92+
}
93+
94+
console.log(checkCashRegister(19.5, 20, [["PENNY", 0.5], ["NICKEL", 0], ["DIME", 0], ["QUARTER", 0], ["ONE", 0], ["FIVE", 0], ["TEN", 0], ["TWENTY", 0], ["ONE HUNDRED", 0]]));

0 commit comments

Comments
 (0)
0