From e674adb156a7c947fa8ab1f37e371409e5966348 Mon Sep 17 00:00:00 2001 From: Soumya Ranjan Date: Sat, 20 Jun 2026 12:50:55 +0530 Subject: [PATCH] no negative value for both bill and tip --- projects/tip-calculator/index.js | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/projects/tip-calculator/index.js b/projects/tip-calculator/index.js index 98cb079..69fff45 100644 --- a/projects/tip-calculator/index.js +++ b/projects/tip-calculator/index.js @@ -4,10 +4,17 @@ const tipInput = document.getElementById("tip"); const totalSpan = document.getElementById("total"); function calculateTotal() { - const billValue = billInput.value; - const tipValue = tipInput.value; + const billValue = Number(billInput.value); + const tipValue = Number(tipInput.value); const totalValue = billValue * (1 + tipValue / 100); totalSpan.innerText = totalValue.toFixed(2); } btnEl.addEventListener("click", calculateTotal); + +billInput.addEventListener("input", () => { + if (billInput.value < 0) billInput.value = 0; +}); +tipInput.addEventListener("input", () => { + if (tipInput.value < 0) tipInput.value = 0; +}); \ No newline at end of file