From 7daa11025bf65819293ea2fc2aeec2cde7c503e7 Mon Sep 17 00:00:00 2001 From: Sanket Bharadwaj <127912897+Sanket-Bharadwaj@users.noreply.github.com> Date: Sun, 5 Oct 2025 01:18:38 +0530 Subject: [PATCH 1/2] Create Calculator.html --- Projects/1-Beginner/Calculator.html | 255 ++++++++++++++++++++++++++++ 1 file changed, 255 insertions(+) create mode 100644 Projects/1-Beginner/Calculator.html diff --git a/Projects/1-Beginner/Calculator.html b/Projects/1-Beginner/Calculator.html new file mode 100644 index 00000000..ec6e0397 --- /dev/null +++ b/Projects/1-Beginner/Calculator.html @@ -0,0 +1,255 @@ + + + + + + Simple Calculator + + + +
+
0
+
+ + + + + + + + + + + + + + + + + + + +
+
+ + + + From c63849e7e525d04b60972fcc9d58efc620d0260e Mon Sep 17 00:00:00 2001 From: Sanket Bharadwaj <127912897+Sanket-Bharadwaj@users.noreply.github.com> Date: Sun, 5 Oct 2025 01:42:07 +0530 Subject: [PATCH 2/2] Update 2.0 Calculator.html --- Projects/1-Beginner/Calculator.html | 27 +++++++++++++++++---------- 1 file changed, 17 insertions(+), 10 deletions(-) diff --git a/Projects/1-Beginner/Calculator.html b/Projects/1-Beginner/Calculator.html index ec6e0397..215091fe 100644 --- a/Projects/1-Beginner/Calculator.html +++ b/Projects/1-Beginner/Calculator.html @@ -82,11 +82,11 @@ - + - + @@ -95,8 +95,9 @@ - + + @@ -190,18 +191,24 @@ clearAll(); return; } + operator = ''; + previousInput = ''; currentInput = ''; - let lastValidInput = ''; + let buffer = ''; for (let item of history) { - if (/[0-9.]/.test(item)) { - currentInput += item; - lastValidInput = currentInput; - } else if (item !== '+/-') { + if (item === '+/-') { + buffer = buffer + ? (buffer.startsWith('-') ? buffer.slice(1) : '-' + buffer) + : '-'; + } else if (/[0-9.]/.test(item)) { + buffer += item; + } else { + previousInput = buffer; operator = item; - previousInput = lastValidInput; - currentInput = ''; + buffer = ''; } } + currentInput = buffer; expression = operator ? `${previousInput}${operator}${currentInput}` : currentInput || '0'; } else { clearAll();