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