|
1 | 1 | const tipPercentButtons = document.querySelectorAll(".tip-percent-btn");
|
| 2 | +const tipInput = document.getElementById("tipInput"); |
| 3 | +const resetBtn = document.getElementById("resetBtn"); |
| 4 | +const allInputs = document.querySelectorAll("input"); |
| 5 | +const billInput = document.getElementById("bill"); |
| 6 | +const peopleInput = document.getElementById("people"); |
| 7 | +const tipPerPersonSpan = document.getElementById("tipPerPerson"); |
| 8 | +const billPerPersonSpan = document.getElementById("billPerPerson"); |
2 | 9 |
|
3 | 10 | let tipPercent = 0;
|
| 11 | +let bill = 0; |
| 12 | +let people = 0; |
4 | 13 |
|
5 | 14 | tipPercentButtons.forEach((tipPercentButton) => {
|
6 | 15 | tipPercentButton.addEventListener("click", (e) => {
|
7 | 16 | e.preventDefault();
|
8 |
| - tipPercentButtons.forEach((tipPercentButton) => { |
9 |
| - tipPercentButton.classList.remove("tip-percent-btn-active"); |
| 17 | + tipInput.value = ""; |
| 18 | + tipPercentButtons.forEach((btn) => { |
| 19 | + btn.classList.remove("tip-percent-btn-active"); |
10 | 20 | });
|
11 |
| - tipPercentButton.classList.toggle("tip-percent-btn-active"); |
12 |
| - tipPercent = tipPercentButton.value; |
| 21 | + tipPercentButton.classList.add("tip-percent-btn-active"); |
| 22 | + tipPercent = parseFloat(tipPercentButton.value); |
| 23 | + calculateBillAndTip(); |
13 | 24 | });
|
14 | 25 | });
|
| 26 | + |
| 27 | +tipInput.addEventListener("focus", () => { |
| 28 | + tipPercentButtons.forEach((button) => { |
| 29 | + button.classList.remove("tip-percent-btn-active"); |
| 30 | + }); |
| 31 | +}); |
| 32 | + |
| 33 | +tipInput.addEventListener("input", () => { |
| 34 | + tipPercent = parseFloat(tipInput.value) || 0; |
| 35 | + calculateBillAndTip(); |
| 36 | +}); |
| 37 | + |
| 38 | +allInputs.forEach((input) => { |
| 39 | + input.addEventListener("input", () => { |
| 40 | + if (resetBtn.classList.contains("inactiveResetBtn")) { |
| 41 | + resetBtn.classList.remove("inactiveResetBtn"); |
| 42 | + resetBtn.classList.add("activeResetBtn"); |
| 43 | + } |
| 44 | + calculateBillAndTip(); |
| 45 | + }); |
| 46 | +}); |
| 47 | + |
| 48 | +resetBtn.addEventListener("click", () => { |
| 49 | + allInputs.forEach((input) => { |
| 50 | + input.value = ""; |
| 51 | + }); |
| 52 | + tipPercentButtons.forEach((btn) => { |
| 53 | + btn.classList.remove("tip-percent-btn-active"); |
| 54 | + }); |
| 55 | + tipPercent = 0; |
| 56 | + bill = 0; |
| 57 | + people = 0; |
| 58 | + tipPerPersonSpan.innerHTML = "0.00"; |
| 59 | + billPerPersonSpan.innerHTML = "0.00"; |
| 60 | + resetBtn.classList.remove("activeResetBtn"); |
| 61 | + resetBtn.classList.add("inactiveResetBtn"); |
| 62 | +}); |
| 63 | + |
| 64 | +billInput.addEventListener("input", () => { |
| 65 | + bill = parseFloat(billInput.value) || 0; |
| 66 | + calculateBillAndTip(); |
| 67 | +}); |
| 68 | + |
| 69 | +peopleInput.addEventListener("input", () => { |
| 70 | + people = parseInt(peopleInput.value) || 0; |
| 71 | + calculateBillAndTip(); |
| 72 | +}); |
| 73 | + |
| 74 | +function calculateBillAndTip() { |
| 75 | + if ( |
| 76 | + bill > 0 && |
| 77 | + bill <= 10000 && |
| 78 | + people > 0 && |
| 79 | + people <= 100 && |
| 80 | + tipPercent >= 0 && |
| 81 | + tipPercent <= 100 |
| 82 | + ) { |
| 83 | + const tipPerPerson = (bill * tipPercent) / (100 * people); |
| 84 | + const billPerPerson = bill / people + tipPerPerson; |
| 85 | + tipPerPersonSpan.innerHTML = tipPerPerson.toFixed(2); |
| 86 | + billPerPersonSpan.innerHTML = billPerPerson.toFixed(2); |
| 87 | + } else { |
| 88 | + tipPerPersonSpan.innerHTML = "0.00"; |
| 89 | + billPerPersonSpan.innerHTML = "0.00"; |
| 90 | + } |
| 91 | +} |
0 commit comments