-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
117 lines (93 loc) · 3.56 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
let tipAmount, total , percentage;
const customButton = document.getElementById('custom');
// ----------------All percentage button---------------
document.getElementById('all%').addEventListener('click', (event) =>{
// console.log(event.target.innerText);
// debugger;
if(event.target.innerText == '5%'){
percentage = 0.05;
calculation();
displayChange();
}
else if(event.target.innerText == '10%'){
percentage = 0.1;
calculation();
displayChange();
}
else if(event.target.innerText == '15%'){
percentage = 0.15;
calculation();
displayChange();
}
else if(event.target.innerText == '25%'){
percentage = 0.25;
calculation();
displayChange();
}
else if(event.target.innerText == '50%'){
percentage = 0.5;
calculation();
displayChange();
}
// -------------for custom button --------------
else if(event.target.tagName.toLowerCase() == 'input' && customButton.value != ''){
if(parseInt(customButton.value) > 0){
percentage = parseInt(customButton.value) / 100;
calculation();
displayChange();
document.getElementById('custom-error').style.display = 'none';
}
else{
document.getElementById('custom-error').style.display = 'block';
event.target.style.border = '2px solid red';
}
}
});
//------------------- full calculation function -------------------
function calculation(){
const bill = parseFloat(document.getElementById('bill-amount').value);
const people = parseInt(document.getElementById('people').value);
if(bill > 0 && people > 0){
tipAmount = parseFloat(bill * percentage);
total = parseFloat(parseFloat(bill + tipAmount)/people);
errorHandleChange('bill-error', 'bill-error-outline');
errorHandleChange('people-error', 'people-error-outline');
}
else if(bill <= 0 || isNaN(bill)){
errorHandle('bill-error', 'bill-error-outline');
tipAmount = 0.00;
total = 0.00;
}
else if(people <= 0 || isNaN(people)){
errorHandle('people-error', 'people-error-outline');
tipAmount = 0.00;
total = 0.00;
}
}
//--------------- error handling function --------------
function errorHandle(id_text, id_outline){
document.getElementById(id_text).style.display = 'block';
document.getElementById(id_outline).style.border = '2px solid red';
}
//--------------- error handling change function --------------
function errorHandleChange(id_text, id_outline){
document.getElementById(id_text).style.display = 'none';
document.getElementById(id_outline).style.border = 'none';
}
//-------------- display change function ----------------------
function displayChange(){
document.getElementById('tip-amount').innerText = tipAmount.toFixed(2);
document.getElementById('total').innerText = total.toFixed(2);
}
//---------------- for Reset button -------------------------
document.getElementById('reset').addEventListener('click', ()=>{
document.getElementById('tip-amount').innerText = '0.00';
document.getElementById('total').innerText = '0.00';
document.getElementById('bill-amount').value = '';
document.getElementById('people').value = '';
document.getElementById('custom').value = '';
errorHandleChange('bill-error', 'bill-error-outline');
errorHandleChange('people-error', 'people-error-outline');
document.getElementById('custom-error').style.display = 'none';
percentage = 0;
});