-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
181 lines (163 loc) · 8.04 KB
/
index.html
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
<!DOCTYPE html>
<html>
<head>
<title> tip calculator</title>
<meta charset="utf-8" content = "width=device-width,initial-scale=1" name="viewport">
<link rel = "stylesheet" href="index.css">
</head>
<body>
<h2 class="top">SPLI</h2>
<h2 class="top">TTER</h2>
<div class="main-content">
<div class="bill">
<h3>bill</h3>
<img class="dollar-icon" src="images/icon-dollar.svg"></img>
<input class="bill-input" id="bill-input" onclick="setZero('bill-input')" oninput="billModified()">
</input>
</div>
<div class="selections">
<h3>Select Tip %</h3>
<div class="button-group">
<button class="normal-button" id="5%" type="button" onclick="buttonModified('5%')">5%</button>
<button class="normal-button" id="10%" type="button" onclick="buttonModified('10%')">10%</button>
<button class="normal-button" id="15%" type="button" onclick="buttonModified('15%')">15%</button>
<button class="normal-button" id="25%" type="button" onclick="buttonModified('25%')">25%</button>
<button class="normal-button" id="50%" type="button" onclick="buttonModified('50%')">50%</button>
<button class="custom-button" id="custom" type="button" onclick="changetoInput()">Custom
</button>
<input class="custom-input" id="custom-input" oninput="customInput()"></input>
</div>
</div>
<div class="people-number">
<h3>Number of People</h3>
<div class="alert"> Can't be Zero</div>
<img class="people-icon" src="images/icon-person.svg"></img>
<input id="people-num" onclick="setZero('people-num')" oninput="NumPeopleModified()"></input>
</div>
<div class="results">
<div class="titles" id="tipAmount">
<h3>Tip Amount</h3>
<div class="per-person">/ person</div>
</div>
<div class="final" id="Tip">
<div class="final-sum" id="tip">
</div>
</div>
<div class="titles" id="totalAmount">
<h3>Total</h3>
<div class="per-person">/ person</div>
</div>
<div class="final" id="Tot">
<div class="final-sum" id="total">
</div>
</div>
<button type="button" class="reset" onclick="reset()">Reset</button>
</div>
</div>
<script>
var bill,percentage,numPeople;
bill = 0.00;
percentage = 0.00;
numPeople = 0.00;
let arr = document.getElementsByTagName("button");
var currentPercent = null;
for(let item of arr){
if(item.className!='reset' && item.id!='custom'){
item.addEventListener("click",()=>{
item.style.backgroundColor = 'hsl(172, 67%, 45%)';
item.style.color = "hsl(183, 100%, 15%)";
if(currentPercent){
currentPercent.style.backgroundColor = 'hsl(183, 100%, 15%)';
currentPercent.style.color = 'white';
}
currentPercent = item;
});
}
}
document.getElementById('bill-input').value = '0';
document.getElementById('people-num').value = '0';
refresh();
function setZero(id){
document.getElementById(id).value="";
}
function showAlert(){
document.getElementsByClassName('alert')[0].style.display = 'block';
document.getElementById('people-num').style.borderColor = 'red';
}
function billModified(){
let input = document.getElementById('bill-input').value;
bill = input;
console.log(bill);
refresh();
}
function buttonModified(str){
let percent = Number(str.split("%")[0]);
percentage = percent;
refresh();
}
function changetoInput(){
document.getElementById('custom').style.display = 'none';
document.getElementById('custom-input').style.display = 'block';
refresh();
}
function customInput(){
percentage = document.getElementById('custom-input').value;
refresh();
}
function NumPeopleModified(){
numPeople = document.getElementById('people-num').value;
if(numPeople=='0'){
showAlert();
}else{
refresh();
}
}
var finalTip,finalSum;
function refresh(){
document.getElementsByClassName('alert')[0].style.display = 'none';
document.getElementById('people-num').style.borderColor = 'hsl(172, 67%, 45%)';
if(bill==0.00&&numPeople==0.00&&percentage==0.00){
finalTip = 0.00;
finalSum = 0.00;
}else{
finalTip = bill/numPeople*percentage/100;
finalSum = bill/numPeople+finalTip;
}
if(!isNaN(finalTip)&&!isNaN(finalSum)&&finalTip!=Infinity&&finalSum!=Infinity && !isNaN(numPeople) &&numPeople!=Infinity)
{
let tip = document.getElementById('tip');
let total = document.getElementById('total');
if(tip.childNodes.length>1&&total.childNodes.length>1){
tip.lastChild.remove();
total.lastChild.remove();
}
let tipNode = document.createTextNode(("$"+finalTip.toFixed(2)).toString());
tip.appendChild(tipNode);
let totalNode = document.createTextNode(("$"+finalSum.toFixed(2)).toString());
total.appendChild(totalNode);
}
}
function reset(){
document.getElementsByClassName('alert')[0].style.display = 'none';
bill = 0;
percentage = 0;
numPeople = 0;
document.getElementById('bill-input').value = '0';
document.getElementById('people-num').value = '0';
document.getElementById('custom-input').style.display = 'none';
document.getElementById('custom').style.backgroundColor='hsl(189, 41%, 97%)';
document.getElementById('custom').style.color = 'hsl(183, 100%, 15%)'
document.getElementById('custom-input').value = '';
document.getElementById('custom').style.display = "block";
let tipNode = document.getElementById('tip').lastChild;
let totalNode = document.getElementById('total').lastChild;
document.getElementById('tip').replaceChild(document.createTextNode('$0.00'),tipNode);
document.getElementById('total').replaceChild(document.createTextNode('$0.00'),totalNode);
if(currentPercent&¤tPercent.id!="custom"){
currentPercent.style.backgroundColor = 'hsl(183, 100%, 15%)';
currentPercent.style.color = 'white';
}
}
</script>
</body>
</html>