Skip to content

Commit ab5ef05

Browse files
authored
Create cashregister.js
1 parent 7345f91 commit ab5ef05

File tree

1 file changed

+58
-0
lines changed

1 file changed

+58
-0
lines changed

5. Cash Register/cashregister.js

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
function checkCashRegister(price, cash, cid) {
2+
const unitMap = {
3+
PENNY: 0.01,
4+
NICKEL: 0.05,
5+
DIME: 0.1,
6+
QUARTER: 0.25,
7+
ONE: 1.0,
8+
FIVE: 5.0,
9+
TEN: 10.0,
10+
TWENTY: 20.0,
11+
"ONE HUNDRED": 100.0
12+
}
13+
14+
let totalCID = 0
15+
for (let money of cid) {
16+
totalCID += money[1]
17+
}
18+
totalCID = totalCID.toFixed(2)
19+
let changeToGive = cash -price
20+
const changeArray = []
21+
22+
if (changeToGive > totalCID) {
23+
return {status: "INSUFFICIENT_FUNDS", change: []}
24+
} else if (changeToGive.toFixed(2) === totalCID){
25+
return {
26+
status: "CLOSED",
27+
change: cid
28+
}
29+
} else {
30+
cid = cid.reverse()
31+
for (let money of cid) {
32+
const [unit,value] = money;
33+
const addition = [unit,0]
34+
35+
while(changeToGive >= unitMap[unit] && value > 0) {
36+
addition[1] += unitMap[unit]
37+
value -= unitMap[unit]
38+
changeToGive -= unitMap[unit]
39+
changeToGive = changeToGive.toFixed(2)
40+
}
41+
if(addition[1] > 0) {
42+
changeArray.push(addition)
43+
}
44+
}
45+
}
46+
47+
if(changeToGive > 0) {
48+
return {status: "INSUFFICIENT_FUNDS", change: []}
49+
}
50+
51+
return {
52+
status: "OPEN",
53+
change: changeArray
54+
}
55+
}
56+
57+
58+
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]])

0 commit comments

Comments
 (0)