Skip to content

Commit ab15eec

Browse files
committed
Soluções finais
1 parent 903b98e commit ab15eec

File tree

1 file changed

+59
-0
lines changed

1 file changed

+59
-0
lines changed

n5/cashRegister.js

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
function checkCashRegister(price, cash, cid) {
2+
let objt =
3+
{
4+
status: "",
5+
change: []
6+
};
7+
8+
const unit = {
9+
"PENNY": 0.01,
10+
"NICKEL": 0.05,
11+
"DIME": 0.1,
12+
"QUARTER": 0.25,
13+
"ONE": 1,
14+
"FIVE": 5,
15+
"TEN": 10,
16+
"TWENTY": 20,
17+
"ONE HUNDRED": 100,
18+
}
19+
20+
let retorno = cash - price;
21+
22+
let somacid = cid.reduce((acc, curr) => acc + curr[1], 0);
23+
let change = [];
24+
let i;
25+
for(i = cid.length-1; i>=0; i--){
26+
let moeda = cid[i][0];
27+
let valorcid = cid[i][1];
28+
let valorunit = unit[moeda];
29+
let x = 0;
30+
while(retorno >= valorunit && valorcid >0){
31+
retorno -= valorunit;
32+
retorno = retorno.toFixed(2);
33+
valorcid -= valorunit;
34+
x++;
35+
}
36+
if(x>0){
37+
change.push([moeda,(valorunit*x)]);
38+
}
39+
40+
}
41+
let somafinal = change.reduce((acc, curr) => acc + curr[1], 0);
42+
somafinal = somafinal - somacid;
43+
44+
45+
46+
if(retorno > 0){
47+
objt.status="INSUFFICIENT_FUNDS";
48+
objt.change=[];
49+
}else if(somafinal == retorno){
50+
objt.status="CLOSED";
51+
objt.change=cid;
52+
}else{
53+
objt.status="OPEN";
54+
objt.change=change;
55+
}
56+
return objt;
57+
}
58+
59+
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)