-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
255 lines (210 loc) · 6.66 KB
/
index.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
document.addEventListener('DOMContentLoaded', () => {
inputBills()
const input = document.querySelector('#askedMoneyInput')
input.addEventListener('click', validateAllFields, false)
document.querySelector('.ux').textContent = 'Ingresa la cantidad de billetes'
document.querySelector('#extractMoney').disabled = true
function validateAllFields(){
const fulfilledInputs = document.querySelectorAll('.fulfilled')
if(fulfilledInputs.length === availableBills.length){
window.extractMoney.disabled = false
const cards = document.querySelectorAll('.card-remaining')
cards.forEach(card => {
document.querySelectorAll('.inputQuantity').forEach(input => {
input.remove()
})
const span = document.createElement('span')
span.classList.add('remainBill')
card.appendChild(span)
})
input.removeEventListener('click', validateAllFields, false)
showAvailableMoney()
} else {
window.alert('Ingrese cantidades permitidas')
}
}
})
class Bill
{
constructor(v, c)
{
this.value = v;
this.quantity = c;
}
}
function moneyDelivery()
{
var askedMoneyInput = document.getElementById("askedMoneyInput");
askedMoney = parseInt(askedMoneyInput.value);
if(askedMoney % 5 !==0 || askedMoney <= 0){
window.alert(`Ingresa una cantidad múltiplo de 5 positivo`)
askedMoneyInput.value = ''
return
}
for(let bill of availableBills)
{
if(askedMoney > 0)
{
div = Math.floor(askedMoney / bill.value);
if(div > bill.quantity)
{
bills = bill.quantity;
}
else
{
bills = div;
}
billsDelivered.push( new Bill(bill.value, bills) );
askedMoney = askedMoney - (bill.value * bills);
}
}
if(askedMoney > 0)
{
window.alert(`Soy un cajero malo, he sido malo y no puedo darte esa cantidad :(`)
askedMoneyInput.value = ''
billsDelivered = []
return
}
else
{
let i = 0
for(var e of billsDelivered)
{
if(e.quantity > 0)
{
for(let i = 0; i < e.quantity; i++){
displayBill(e.value)
}
}
availableBills[i].quantity -= e.quantity
i++
}
document.querySelectorAll('.remainBill').forEach((bill, index) => {
bill.textContent = `X ${availableBills[index].quantity}`
})
showAvailableMoney()
billsDelivered = []
}
// showing vertical scroll
document.querySelector('.bills-container').style['overflow-y'] = 'scroll'
// showing info
document.querySelector('.info').style['display'] = 'inline'
document.querySelector('.results').style['display'] = 'flex'
}
function displayBill(value) {
const oneHundred = document.querySelector('.one-hundred')
const fifty = document.querySelector('.fifty')
const twenty = document.querySelector('.twenty')
const ten = document.querySelector('.ten')
const five = document.querySelector('.five')
const input = document.querySelector('#askedMoneyInput')
const button = document.querySelector('#extractMoney')
let bill = ''
let id = ''
switch(value){
case 100:{
bill = oneHundred
id = 'oneHundred'
break
}
case 50:{
bill = fifty
id = 'fifty'
break
}
case 20:{
bill = twenty
id = 'twenty'
break
}
case 10:{
bill = ten
id = 'ten'
break
}
case 5:{
bill = five
id = 'five'
break
}
}
// adding element
const billImg = document.createElement('img')
billImg.src = `./images/${value}.png`
billImg.id = id
billImg.classList.add('bill')
bill.append(billImg)
//quantities
showInfo()
//reset
input.addEventListener('click', reset, true)
button.addEventListener('click', reset, true)
}
function reset(){
const input = document.querySelector('#askedMoneyInput')
const button = document.querySelector('#extractMoney')
input.removeEventListener('click', reset, true)
button.removeEventListener('click', reset, true)
input.value = ''
// remove elements
document.querySelectorAll('.bill').forEach(img => img.remove())
// hide vertical scroll
document.querySelector('.bills-container').style['overflow-y'] = 'hidden'
document.querySelector('.info').style['display'] = 'none'
document.querySelector('.results').style['display'] = 'flex'
button.addEventListener('click', moneyDelivery, false)
showInfo()
}
function showInfo(){
document.querySelector('#askedMoneyInput').value = ''
window.quantityOneHundred.textContent = `X ${document.querySelectorAll('#oneHundred').length}`
window.quantityFifty.textContent = `X ${document.querySelectorAll('#fifty').length}`
window.quantityTwenty.textContent = `X ${document.querySelectorAll('#twenty').length}`
window.quantityTen.textContent = `X ${document.querySelectorAll('#ten').length}`
window.quantityFive.textContent = `X ${document.querySelectorAll('#five').length}`
window.total.textContent = `${document.querySelectorAll('.bill').length}`
}
function showAvailableMoney(){
document.querySelector('.ux').textContent = 'Billetes disponibles'
let total = 0
availableBills.forEach(bill => {
total += (bill.value * bill.quantity)
})
document.querySelectorAll('.remainBill').forEach((bill, index) => {
bill.textContent = `X ${availableBills[index].quantity}`
})
document.querySelector('.available-money').textContent = `Dinero disponible: $${total > 0 ? total : 0}`
}
function inputBills(){
const typesOfBills = [100, 50, 20, 10, 5]
for(bill in typesOfBills){
availableBills.push(new Bill(typesOfBills[bill], 0))
}
const bills = document.querySelectorAll('.inputQuantity')
bills.forEach((bill, index) => {
bill.onfocus = function(){
validateInput(bill)
}
bill.addEventListener('change', event => {
validateInput(bill)
if(availableBills[index].value === typesOfBills[index]){
availableBills[index].quantity = Number(event.target.value)
} else{
availableBills.push(new bill(typesOfBills[index], Number(event.target.value)))
}
showAvailableMoney()
})
})
}
function validateInput(input){
if(input.value !== '' && input.value >= 0){
input.classList.remove('invalid')
input.classList.add('fulfilled')
}else{
input.classList.remove('fulfilled')
input.classList.add('invalid')
}
}
let availableBills = []
let billsDelivered = []
document.getElementById("extractMoney").addEventListener("click", moneyDelivery);