-
Notifications
You must be signed in to change notification settings - Fork 88
/
script.js
125 lines (110 loc) · 3.16 KB
/
script.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
let price = 3.26;
let cid = [
['PENNY', 1.01],
['NICKEL', 2.05],
['DIME', 3.1],
['QUARTER', 4.25],
['ONE', 90],
['FIVE', 55],
['TEN', 20],
['TWENTY', 60],
['ONE HUNDRED', 100]
];
const displayChangeDue = document.getElementById('change-due');
const cash = document.getElementById('cash');
const purchaseBtn = document.getElementById('purchase-btn');
const priceScreen = document.getElementById('price-screen');
const cashDrawerDisplay = document.getElementById('cash-drawer-display');
const formatResults = (status, change) => {
displayChangeDue.innerHTML = `<p>Status: ${status}</p>`;
change.map(
money => (displayChangeDue.innerHTML += `<p>${money[0]}: $${money[1]}</p>`)
);
return;
};
const checkCashRegister = () => {
if (Number(cash.value) < price) {
alert('Customer does not have enough money to purchase the item');
cash.value = '';
return;
}
if (Number(cash.value) === price) {
displayChangeDue.innerHTML =
'<p>No change due - customer paid with exact cash</p>';
cash.value = '';
return;
}
let changeDue = Number(cash.value) - price;
let reversedCid = [...cid].reverse();
let denominations = [100, 20, 10, 5, 1, 0.25, 0.1, 0.05, 0.01];
let result = { status: 'OPEN', change: [] };
let totalCID = parseFloat(
cid
.map(total => total[1])
.reduce((prev, curr) => prev + curr)
.toFixed(2)
);
if (totalCID < changeDue) {
return (displayChangeDue.innerHTML = '<p>Status: INSUFFICIENT_FUNDS</p>');
}
if (totalCID === changeDue) {
formatResults('CLOSED', cid);
}
for (let i = 0; i <= reversedCid.length; i++) {
if (changeDue > denominations[i] && changeDue > 0) {
let count = 0;
let total = reversedCid[i][1];
while (total > 0 && changeDue >= denominations[i]) {
total -= denominations[i];
changeDue = parseFloat((changeDue -= denominations[i]).toFixed(2));
count++;
}
result.change.push([reversedCid[i][0], count * denominations[i]]);
}
}
if (changeDue > 0) {
return (displayChangeDue.innerHTML = '<p>Status: INSUFFICIENT_FUNDS</p>');
}
formatResults(result.status, result.change);
updateUI(result.change);
};
const checkResults = () => {
if (!cash.value) {
return;
}
checkCashRegister();
};
const updateUI = change => {
const currencyNameMap = {
PENNY: 'Pennies',
NICKEL: 'Nickels',
DIME: 'Dimes',
QUARTER: 'Quarters',
ONE: 'Ones',
FIVE: 'Fives',
TEN: 'Tens',
TWENTY: 'Twenties',
'ONE HUNDRED': 'Hundreds'
};
// Update cid if change is passed in
if (change) {
change.forEach(changeArr => {
const targetArr = cid.find(cidArr => cidArr[0] === changeArr[0]);
targetArr[1] = parseFloat((targetArr[1] - changeArr[1]).toFixed(2));
});
}
cash.value = '';
priceScreen.textContent = `Total: $${price}`;
cashDrawerDisplay.innerHTML = `<p><strong>Change in drawer:</strong></p>
${cid
.map(money => `<p>${currencyNameMap[money[0]]}: $${money[1]}</p>`)
.join('')}
`;
};
purchaseBtn.addEventListener('click', checkResults);
cash.addEventListener('keydown', e => {
if (e.key === 'Enter') {
checkResults();
}
});
updateUI();