-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
131 lines (86 loc) · 2.81 KB
/
Copy pathscript.js
File metadata and controls
131 lines (86 loc) · 2.81 KB
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
const billInput = document.querySelector('#bill-input');
let bill = 0;
const tipButtons = document.querySelectorAll('.tip-button');
const customTipInput = document.querySelector('#custom-tip-input');
let tipPerc = 0.05;
const peopleInput = document.querySelector('#people-input');
let people = 0;
const tipResult = document.querySelector('#tip-result');
const totalResult = document.querySelector('#total-result');
// EVENT LISTENERS //
billInput.addEventListener('input', function () {
bill = parseFloat(billInput.value);
if (bill !== 0 && !isNaN(bill)) {
removeErrorMessage('bill')
calculateTip();
} else {
showErrorMessage('bill');
}
})
tipButtons.forEach(item => {
item.addEventListener('click', function () {
const selectedButton = document.querySelector('.selected');
if (selectedButton !== item) {
selectedButton.classList.remove('selected');
item.classList.add('selected');
}
tipPerc = parseFloat(item.value);
calculateTip();
})
})
customTipInput.addEventListener('input', function () {
const selectedButton = document.querySelector('.selected');
selectedButton.classList.remove('selected');
tipPerc = parseFloat(customTipInput.value / 100);
calculateTip();
})
peopleInput.addEventListener('input', function () {
people = parseFloat(peopleInput.value);
if (people !== 0 && !isNaN(people)) {
removeErrorMessage('people')
calculateTip();
} else {
showErrorMessage('people');
}
})
// FUNCTION TO CALCULATE TIP //
function calculateTip() {
if (people !== 0 && bill !== 0) {
let tip = bill * tipPerc;
let tipPerPerson = tip / people;
let totalPerPerson = (bill + tip) / people;
tipResult.textContent = tipPerPerson.toFixed(2);
totalResult.textContent = totalPerPerson.toFixed(2);
}
}
// ERROR MESSAGES //
function showErrorMessage(inputField) {
const input = document.querySelector(`#${inputField}-input`);
const errorMessage = document.querySelector(`#${inputField}-error-message`);
errorMessage.style.display = 'block';
if (!input.parentElement.classList.contains('input-error')) {
input.parentElement.classList.add('input-error');
}
}
function removeErrorMessage(inputField) {
const input = document.querySelector(`#${inputField}-input`);
const errorMessage = document.querySelector(`#${inputField}-error-message`);
errorMessage.style.display = 'none';
if (input.parentElement.classList.contains('input-error')) {
input.parentElement.classList.remove('input-error');
}
}
// RESET BUTTON
const resetButton = document.querySelector('#reset-button');
resetButton.addEventListener('click', function () {
reset();
})
function reset() {
billInput.value = '';
bill = 0;
tipPerc = 0;
peopleInput.value = '';
people = 0;
tipResult.textContent = 0;
totalResult.textContent = 0;
}