-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
123 lines (106 loc) · 3.4 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
const billInput = document.querySelector('#bill_amount');
const numPeople = document.querySelector('#totalPeople');
const otherTip = document.querySelector('.btn_custom');
const tipAmount = document.querySelector("#finalAmount");
const finalAmount = document.querySelector("#totalAmount");
const resetBtn = document.querySelector('.btn_reset');
// update the views
function updateNumbers(tip, split) {
if (tip == Infinity || split == Infinity || isNaN(tip) || isNaN(split)) {
tipAmount.innerText = "$0.00";
finalAmount.innerText = "$0.00";
} else {
tipAmount.innerText = `${tip.toLocaleString('en-US', {
style: 'currency',
currency: 'USD',
minimumFractionDigits: 0,
maxmimumFractionDigits: 2
})}`;
finalAmount.innerText = `${split.toLocaleString('en-US', {
style: 'currency',
currency: 'USD',
minimumFractionDigits: 0,
maximumFractionDigits: 2
})}`;
tipAmount.animate([
{ color: 'var(--clr-neutral-other)' },
{ color: 'var(--clr-primary-highlight)' }
], 1500)
finalAmount.animate([
{ color: 'var(--clr-neutral-other)' },
{ color: 'var(--clr-primary-highlight)' }
], 1500)
resetBtn.removeAttribute("disabled");
}
}
// Listen to tip inputs
const allTipBtns = document.querySelector(".tip_option_buttons");
let tipBtnSelection = "";
allTipBtns.addEventListener('click', (e) => {
let selectedOption = e.target;
if (tipBtnSelection) {
tipBtnSelection.classList.remove('selectedOption');
}
tipBtnSelection = selectedOption
selectedOption.classList.add('selectedOption');
if (selectedOption.type == "button") {
tipCalc(+billInput.value, +selectedOption.value);
otherTip.value = otherTip.defaultValue;
} else {
customTip(+selectedOption.value);
}
})
// custom tip button
function customTip(customValue) {
let convertedCustomTip = customValue / 100;
tipCalc(+billInput.value, convertedCustomTip)
}
otherTip.addEventListener('input', () => {
if (+billInput.value) {
customTip(+otherTip.value);
}
})
// Listen to input values
billInput.addEventListener('input', () => {
numValidation(billInput)
})
// people
numPeople.addEventListener('input', () => {
numPeople.parentElement.classList.remove('show');
numValidation(numPeople)
})
// calculate tip
function tipCalc(billAmount, selectedTip) {
let tipAmt = (billAmount * selectedTip);
let totalBill = tipAmt + billAmount;
let billSplit = totalBill / (+numPeople.value);
let tipSplit = tipAmt / (+numPeople.value)
updateNumbers(tipSplit, billSplit);
}
// number validation
function numValidation(inputNum) {
if (inputNum.value < 0) {
inputNum.value = inputNum.defaultValue;
} else if (inputNum.value == 0) {
inputNum.parentElement.classList.add('show');
} else if (inputNum.value) {
if (billInput.value && tipBtnSelection.type == "button") {
tipCalc(+billInput.value, +tipBtnSelection.value);
} else if (billInput.value && otherTip.value) {
customTip(+otherTip.value)
}
}
}
// reset button
resetBtn.addEventListener('click', reset);
function reset() {
if (!resetBtn.hasAttribute('disabled')) {
billInput.value = billInput.defaultValue;
numPeople.value = numPeople.defaultValue;
otherTip.value = otherTip.defaultValue;
tipBtnSelection.classList.remove('selectedOption');
tipAmount.innerText = "$0.00";
finalAmount.innerText = "$0.00";
resetBtn.setAttribute("disabled", true);
}
}