-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
40 lines (34 loc) · 1.62 KB
/
main.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
// Get references to form elements
const billTotalInput = document.getElementById('billTotal');
const tipInput = document.getElementById('tipInput');
const tipPercentageInput = document.getElementById('tipPercentage');
const tipAmountInput = document.getElementById('tipAmount');
const totalBillInput = document.getElementById('totalBill');
// Function to calculate tip amount and total bill
function calculateTip() {
// Get the values from input fields
const billTotal = parseFloat(billTotalInput.value);
const tipPercentage = parseInt(tipInput.value);
// Calculate tip amount and total bill
const tipAmount = (billTotal * tipPercentage) / 100;
const totalBill = billTotal + tipAmount;
// Update disabled input fields with calculated values
tipPercentageInput.value = tipPercentage + '%';
tipAmountInput.value = tipAmount.toFixed(2);
totalBillInput.value = totalBill.toFixed(2);
}
// Event listeners for input and change events
// Event listener for input event on billTotalInput
billTotalInput.addEventListener('input', function() {
// Get the value entered by the user
let billTotalValue = billTotalInput.value;
// Check if the value has more than two decimal places
const decimalIndex = billTotalValue.indexOf('.');
if (decimalIndex !== -1 && billTotalValue.length - decimalIndex > 3) {
// Truncate the value to two decimal places
billTotalValue = parseFloat(billTotalValue).toFixed(2);
// Update the input field with the truncated value
billTotalInput.value = billTotalValue;
}
});
tipInput.addEventListener('input', calculateTip);