-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.coffee
77 lines (73 loc) · 2.49 KB
/
main.coffee
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
names = document.getElementById 'name' #order name
quantity = document.getElementById 'quantity' #order quantity
price = document.getElementById 'price' #order price
tax = document.getElementById 'tax' #order tax in percent
orderList = document.getElementById 'orders' #list of orders
orders = [] #orders array
window.addOrder = ->
if (
names.value and
not isNaN(parseInt quantity.value) and
(not isNaN(parseFloat price.value) and not isNaN parseFloat tax.value) #make sure all inputs are valid
)
orders.push
#add order to list of orders
names: names.value
quantity: parseInt quantity.value
price: parseInt(price.value * 100) / 100
tax: parseInt(tax.value * 100) / 100
names.value = '' #set all input values to blank (empty text)
quantity.value = ''
price.value = ''
tax.value = ''
updateList() #update order list for each record
updateList = ->
total = orders.reduce(
(totalPrice, order) ->
totalPrice + order.quantity * order.price * (1 + order.tax / 100)
,
0
)
html =
'<table><tr><th>Name</th><th>Quantity</th><th>Price</th><th>Tax</th><th>Total</th><th></th></tr>' #set HTML content output for all order records
orders.forEach (order, i) ->
html += '<tr><td>' + order.names + '</td>'
html += '<td style="text-align: right;">' + order.quantity + '</td>'
html +=
'<td style="text-align: right;">$' +
order.price.toLocaleString(
'en-US'
minimumFractionDigits: 2, maximumFractionDigits: 2
) +
'</td>'
html +=
'<td style="text-align: right;">' +
order.tax.toLocaleString(
'en-US'
minimumFractionDigits: 2, maximumFractionDigits: 2
) +
'%</td>'
html +=
'<td style="text-align: right;">$' +
(order.quantity * order.price * (1 + order.tax / 100)).toLocaleString(
'en-US'
minimumFractionDigits: 2, maximumFractionDigits: 2
) +
'</td>'
html +=
'<td><button onclick="deleteOrder(' + i + ')">Delete</button></td></tr>'
html +=
'<tr><td colspan="4"></td><td style="text-align: right;">Total: $' +
total.toLocaleString(
'en-US'
minimumFractionDigits: 2, maximumFractionDigits: 2
) +
'</td><td></td></tr>'
html += '</table>'
orderList.innerHTML = html #set order list HTML output
window.deleteOrder = (i) ->
if confirm(
'Are you sure you want to delete this task named ' + orders[i].names + '?'
)
orders.splice i, 1 #remove order from index i
updateList()