forked from sumn2u/learn-javascript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
114 lines (97 loc) · 3.73 KB
/
script.js
File metadata and controls
114 lines (97 loc) · 3.73 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
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
// Get DOM elements
const billAmountInput = document.getElementById('bill-amount');
const tipButtons = document.querySelectorAll('.tip-btn');
const customTipInput = document.getElementById('custom-tip');
const numPeopleInput = document.getElementById('num-people');
const calculateBtn = document.getElementById('calculate-btn');
const tipAmountSpan = document.getElementById('tip-amount');
const totalAmountSpan = document.getElementById('total-amount');
const perPersonDiv = document.getElementById('per-person');
const amountPerPersonSpan = document.getElementById('amount-per-person');
// Variables to store current values
let billAmount = 0;
let tipPercentage = 20; // Default to 20%
let numPeople = 1;
// Function to validate numeric input
function validateNumericInput(input, min = 0) {
const value = parseFloat(input.value);
if (isNaN(value) || value < min) {
input.style.borderColor = '#e74c3c';
return false;
} else {
input.style.borderColor = '#ddd';
return true;
}
}
// Function to update tip percentage from buttons
function updateTipPercentage(percentage) {
tipPercentage = percentage;
customTipInput.value = '';
// Update active button
tipButtons.forEach(btn => {
btn.classList.remove('active');
if (parseInt(btn.dataset.tip) === percentage) {
btn.classList.add('active');
}
});
}
// Function to calculate tip and total
function calculateTip() {
if (!validateNumericInput(billAmountInput, 0)) {
alert('Please enter a valid bill amount.');
return;
}
billAmount = parseFloat(billAmountInput.value);
// Check if custom tip is entered
if (customTipInput.value !== '') {
if (!validateNumericInput(customTipInput, 0)) {
alert('Please enter a valid tip percentage.');
return;
}
tipPercentage = parseFloat(customTipInput.value);
tipButtons.forEach(btn => btn.classList.remove('active'));
}
numPeople = parseInt(numPeopleInput.value) || 1;
const tipAmount = (billAmount * tipPercentage) / 100;
const totalAmount = billAmount + tipAmount;
// Animate result updates
animateResult(tipAmountSpan, tipAmount.toFixed(2));
animateResult(totalAmountSpan, totalAmount.toFixed(2));
// Handle split bill
if (numPeople > 1) {
const amountPerPerson = totalAmount / numPeople;
perPersonDiv.style.display = 'block';
animateResult(amountPerPersonSpan, amountPerPerson.toFixed(2));
} else {
perPersonDiv.style.display = 'none';
}
}
// Function to animate result updates
function animateResult(element, newValue) {
element.textContent = '$' + newValue;
element.parentElement.classList.add('updated');
setTimeout(() => {
element.parentElement.classList.remove('updated');
}, 500);
}
// Event listeners for tip buttons
tipButtons.forEach(button => {
button.addEventListener('click', () => {
updateTipPercentage(parseInt(button.dataset.tip));
});
});
// Event listener for custom tip input
customTipInput.addEventListener('input', () => {
if (customTipInput.value !== '') {
tipButtons.forEach(btn => btn.classList.remove('active'));
tipPercentage = parseFloat(customTipInput.value) || 0;
}
});
// Event listener for calculate button
calculateBtn.addEventListener('click', calculateTip);
// Event listeners for real-time validation
billAmountInput.addEventListener('input', () => validateNumericInput(billAmountInput, 0));
customTipInput.addEventListener('input', () => validateNumericInput(customTipInput, 0));
numPeopleInput.addEventListener('input', () => validateNumericInput(numPeopleInput, 1));
// Initialize with default values
updateTipPercentage(20);