Skip to content

Commit c9b32c6

Browse files
committed
bullseye
1 parent 67c734d commit c9b32c6

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

Cash_Register/script.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
function checkCashRegister(price, cash, cid) {
2+
let change = cash * 100 - price * 100;
3+
let cidTotal = 0;
4+
for (let element of cid) {
5+
cidTotal += element[1] * 100
6+
}
7+
if (change > cidTotal) {
8+
return { status: "INSUFFICIENT_FUNDS", change: [] }
9+
} else if (change === cidTotal) {
10+
return { status: "CLOSED", change: cid }
11+
} else {
12+
let answer = [];
13+
cid = cid.reverse();
14+
let moneyUnits = { "ONE HUNDRED": 10000, "TWENTY": 2000, "TEN": 1000, "FIVE": 500, "ONE": 100, "QUARTER": 25, "DIME": 10, "NICKEL": 5, "PENNY": 1 }
15+
for (let element of cid) {
16+
let holder = [element[0], 0];
17+
element[1] = element[1]*100;
18+
while (change >= moneyUnits[element[0]] && element[1] > 0){
19+
change -= moneyUnits[element[0]];
20+
element[1] -= moneyUnits[element[0]];
21+
holder[1] += moneyUnits[element[0]]/100;
22+
}
23+
if (holder[1] > 0){
24+
answer.push(holder)
25+
26+
}
27+
}
28+
if (change > 0){
29+
return { status: "INSUFFICIENT_FUNDS", change: [] }
30+
}
31+
return { status: "OPEN", change: answer }
32+
}
33+
}
34+
35+
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)