-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
91 lines (81 loc) · 3.17 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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="styles.css">
<link href="https://fonts.googleapis.com/css2?family=Roboto:wght@500&display=swap" rel="stylesheet" />
<title>Quick Tip</title>
</head>
<body>
<main oninput="update()">
<h1>Quick Tip</h1>
<section>
<div class="bill">
<label for="yourBill">Bill</label>
<input type="number" placeholder="Your bill" id="yourBill" />
</div>
<div>
<div class="space-between">
<label for="tipInput">Select tip</label>
<span id="tipPercent"></span>
</div>
<input type="range" value="0" id="tipInput" class="range" />
</div>
<div class="space-between">
<span>Tip</span>
<span id="tipValue"></span>
</div>
<hr />
<div class="space-between total">
<span>Total</span>
<span id="totalWithTip"></span>
</div>
</section>
<section>
<div>
<div class="space-between">
<label for="splitInput">Split</label>
<span id="splitValue">1 person</span>
</div>
<input type="range" min="1" max="10" value="1" id="splitInput" class="range" />
</div>
<div class="space-between">
<span>Bill each</span>
<span id="billEach"></span>
</div>
<div class="space-between">
<span>Tip each</span>
<span id="tipEach"></span>
</div>
</section>
</main>
<script>
function formatMoney(value) {
value = Math.ceil(value * 100) / 100;
value = value.toFixed(2);
return "$ " + value;
}
function formatSplit(value) {
if (value === "1") return value + "person";
return value + " people"
}
function update() {
let bill = Number(document.getElementById("yourBill").value);
let tipPercent = document.getElementById("tipInput").value;
let split = document.getElementById("splitInput").value;
let tipValue = bill * (tipPercent / 100);
let tipEach = tipValue / split;
let newBillEach = (bill + tipValue) / split;
document.getElementById("tipPercent").innerHTML = tipPercent + "%";
document.getElementById("tipValue").innerHTML = formatMoney(tipValue);
document.getElementById("totalWithTip").innerHTML = formatMoney(bill + tipValue);
document.getElementById("splitValue").innerHTML = formatSplit(split);
document.getElementById("billEach").innerHTML = formatMoney(newBillEach);
document.getElementById("tipEach").innerHTML = formatMoney(tipEach);
}
let container = document.getElementById("container");
container.addEventListener("input", update);
</script>
</body>
</html>