repl.it
JavaScript

No description

fork
loading
main.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
function checkCashRegister(price, cash, cid) {

var newCid = [];

for (var j=0; j<9; j++){
  newCid.push(cid[j].slice())
}



var unit = [["PENNY", 0.01],["NICKEL", 0.05],["DIME", 0.1],["QUARTER", 0.25],["ONE", 1],["FIVE", 5],["TEN", 10],["TWENTY", 20],["ONE HUNDRED", 100]]

var cashRegisterInverted = cid.slice(0)
cashRegisterInverted.reverse()

var total = 0;
var change = cash - price;
change = Math.round(change*100)/100
var reversed = unit.slice(0).reverse();
var currencyAmount = 0;
var register = []
var temp = change;
var a = 0;



for (var i= 0; i<unit.length; i++){
   total += cid[i][1]
}   
                   


var tempUnit = 0;



for (var u=0; u<9; u++){
  if (temp >= reversed[u][1] && cashRegisterInverted[u][1]>0){
    
    tempUnit = Math.floor(Math.round((temp/reversed[u][1])*100)/100)*Math.round(reversed[u][1]*100)/100


    // Math.round(change*100)/100

    if (cashRegisterInverted[u][1] >= tempUnit){
    register.push([reversed[u][0],tempUnit]);
    temp = temp - tempUnit
    a = a + tempUnit;
    cashRegisterInverted[u][1] = cashRegisterInverted[u][1] - tempUnit
    }


    else if (cashRegisterInverted [u][1] < tempUnit){
      register.push([reversed[u][0],cashRegisterInverted[u][1]]);
      temp = temp - cashRegisterInverted[u][1];
      a = a + cashRegisterInverted [u][1];
      cashRegisterInverted[u][1] = 0
    }
    else if (a == change){
      break
    }
  }
}


for (var j= 0; j <9; j++){
  currencyAmount += cashRegisterInverted[j][1]/reversed[j][1]
     };



a = Math.round(a*100)/100




//console.log("This is the current amount of each currency unit: " + '\n' + reversed.reverse() + '\n')
console.log("My register have the following amount: " + '\n'+ cashRegisterInverted.reverse() + '\n')
// console.log ("So this is the current amount i have: " + '\n'+ currencyAmount.reverse() + '\n')
console.log ("The change is " + change)
console.log( "My cash register still have " + currencyAmount + " dollars")
console.log("I have " + a + " to give")
console.log(register)


var status = ""




if (change == a && currencyAmount > 0){
  status = "OPEN"
  return {status:status, change:register };
}

else if (change == a && currencyAmount == 0){
  status = "CLOSED"
  return {status:status, change:newCid };
}

else if (change > a){
  status = "INSUFFICIENT_FUNDS"
  return {status:status, change:[] };
}


return Math.floor(0.04/reversed[8][1])*reversed[8][1]

}

     
console.log(JSON.stringify(checkCashRegister(19.5, 20, [["PENNY", 0.5], ["NICKEL", 0], ["DIME", 0], ["QUARTER", 0], ["ONE", 0], ["FIVE", 0], ["TEN", 0], ["TWENTY", 0], ["ONE HUNDRED", 0]])));
Native Browser JavaScript
?