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] 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();