Skip to content

Commit 93fa6cd

Browse files
committed
Final javascript project #5
1 parent dd05edf commit 93fa6cd

File tree

1 file changed

+62
-0
lines changed

1 file changed

+62
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
function checkCashRegister(price, cash, cid) {
2+
//const cidTotal = cid[0][1]*0.01+cid[1][1]*0.05+cid[2][1]*0.1+cid[3][1]*0.25+cid[4][1]*1+cid[5][1]*5+cid[6][1]*10+cid[7][1]*20+cid[8][1]*100;
3+
let currency = [
4+
["ONE HUNDRED", 100.00],
5+
["TWENTY", 20.00],
6+
["TEN", 10.00],
7+
["FIVE", 5.00],
8+
["ONE", 1.00],
9+
["QUARTER", 0.25],
10+
["DIME", 0.10],
11+
["NICKEL", 0.05],
12+
["PENNY", 0.01]
13+
];
14+
15+
let change = (cash-price).toFixed(2);
16+
let cidTotal = 0;
17+
for (let element of cid) {
18+
cidTotal += element[1];
19+
}
20+
cidTotal.toFixed(2);
21+
22+
const output = {
23+
status: "",
24+
change: []
25+
}
26+
27+
if (change == cidTotal){
28+
output.status = "CLOSED";
29+
output.change = cid;
30+
}
31+
32+
else if (change > cidTotal){
33+
output.status = "INSUFFICIENT_FUNDS";
34+
}
35+
36+
else{
37+
cid.reverse();
38+
for (let i in currency){
39+
let returnMoney = 0;
40+
//let noOfBills = cid[i][1]/currency[i][1];
41+
while(change >= currency[i][1] && cid[i][1]>0 && change!==0){
42+
change -= currency[i][1];
43+
cid[i][1] -= currency[i][1];
44+
returnMoney += currency[i][1];
45+
change = Math.round(change * 100) / 100;
46+
//noOfBills--
47+
}
48+
if(returnMoney>0) {
49+
output.change.push([currency[i][0], returnMoney])
50+
output.status = "OPEN";
51+
}
52+
}
53+
if (change>0){
54+
output.status = "INSUFFICIENT_FUNDS";
55+
output.change = [];
56+
}
57+
}
58+
console.log(output)
59+
return output;
60+
}
61+
62+
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)