-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
28 lines (23 loc) · 1.01 KB
/
script.js
File metadata and controls
28 lines (23 loc) · 1.01 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
document.addEventListener('DOMContentLoaded', function() {
const billTotal = document.getElementById('bill-total');
const tipInput = document.getElementById('tip');
const tipPercentage = document.getElementById('tip-percentage');
const tipAmount = document.getElementById('tip-amount');
const totalWithTip = document.getElementById('total-with-tip');
const tipForm = document.getElementById('tip-form');
tipForm.addEventListener('input', updateTip);
function updateTip() {
const billAmount = parseFloat(billTotal.value);
const tipValue = parseFloat(tipInput.value);
if (isNaN(billAmount)) {
alert('Please enter a valid number in the Bill Total field.');
return;
}
const tipPercent = tipValue;
const tip = (billAmount * tipPercent) / 100;
const total = billAmount + tip;
tipPercentage.value = tipPercent.toFixed(2) + '%';
tipAmount.value = tip.toFixed(2);
totalWithTip.value = total.toFixed(2);
}
});