|
| 1 | +const btnEl = document.getElementById("calculate"); |
| 2 | +const resetBtnEl = document.getElementById("reset"); |
| 3 | +const billInput = document.getElementById("bill"); |
| 4 | +const tipInput = document.getElementById("tip"); |
| 5 | +const splitInput = document.getElementById("split"); |
| 6 | +const totalSpan = document.getElementById("total"); |
| 7 | +const tipAmountSpan = document.getElementById("tipAmount"); |
| 8 | +const amountPerPersonSpan = document.getElementById("amountPerPerson"); |
| 9 | + |
| 10 | +function calculateTotal() { |
| 11 | + const billValue = billInput.value; |
| 12 | + const tipValue = tipInput.value; |
| 13 | + const splitValue = splitInput.value; |
| 14 | + |
| 15 | + const tipAmount = billValue * (tipValue / 100); |
| 16 | + const totalValue = parseFloat(billValue) + tipAmount; |
| 17 | + |
| 18 | + totalSpan.innerText = totalValue.toFixed(2); |
| 19 | + tipAmountSpan.innerText = tipAmount.toFixed(2); |
| 20 | + |
| 21 | + if (splitValue && splitValue > 0) { |
| 22 | + const amountPerPerson = totalValue / splitValue; |
| 23 | + amountPerPersonSpan.innerText = amountPerPerson.toFixed(2); |
| 24 | + } else { |
| 25 | + amountPerPersonSpan.innerText = ""; |
| 26 | + } |
| 27 | +} |
| 28 | + |
| 29 | +function resetValues() { |
| 30 | + billInput.value = ""; |
| 31 | + tipInput.value = ""; |
| 32 | + splitInput.value = ""; |
| 33 | + totalSpan.innerText = ""; |
| 34 | + tipAmountSpan.innerText = ""; |
| 35 | + amountPerPersonSpan.innerText = ""; |
| 36 | +} |
| 37 | + |
| 38 | +btnEl.addEventListener("click", calculateTotal); |
| 39 | +resetBtnEl.addEventListener("click", resetValues); |
0 commit comments