-
Notifications
You must be signed in to change notification settings - Fork 0
/
app-destructuring.js
70 lines (58 loc) · 1.88 KB
/
app-destructuring.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
const cost = document.querySelector('#cost')
const tipPercent = document.querySelector('#tip-percent')
const tip = document.querySelector('#tip')
const persons = document.querySelector('#persons')
const costPerPerson = document.querySelector('#cost-per-person')
const total = document.querySelector('#total')
cost.addEventListener('keydown', grandTotal)
cost.addEventListener('keyup', grandTotal)
tipPercent.addEventListener('keydown', grandTotal)
tipPercent.addEventListener('keyup', grandTotal)
persons.addEventListener('keydown', grandTotal)
persons.addEventListener('keyup', grandTotal)
//The TIP Buttons:
const tipButtons = document.querySelectorAll('.tip-btn')
Array.from(tipButtons).forEach(link => {
link.addEventListener('click', tipCalcBtn)
})
function tipCalcBtn() {
tipPercent.value = this.value
blueClassRemover(tipButtons)
this.classList.add('pure-button-primary')
grandTotal()
}
//The Person Buttons:
const personButtons = document.querySelectorAll('.person-btn')
Array.from(personButtons).forEach(link => {
link.addEventListener('click', personCalcBtn)
})
function personCalcBtn() {
persons.value = this.value
blueClassRemover(personButtons)
this.classList.add('pure-button-primary')
grandTotal()
}
//Toggles blue button class
function blueClassRemover(hereBtn) {
Array.from(hereBtn).forEach(link =>
link.classList.remove('pure-button-primary')
)
}
//Calculations
function calculations() {
tip.value = Math.round(cost.value * (tipPercent.value / 100))
costPerPerson.value = Math.ceil(total.value / persons.value)
total.value = Number(cost.value) + Number(tip.value)
}
function grandTotal() {
calculations()
calculations()
}
//With destructuring, TO-DO, keep learning from ES6 from Wes Boss:
function mainCalc(
{ cost = 100, tipper: tipPercent = 15, persons = 1, total = 115 } = {}
) {
return cost + tipPercent
}
const bill = mainCalc({ tipper: 20, cost: 400 })
console.log(bill)