diff --git a/script.js b/script.js new file mode 100644 index 0000000..f637294 --- /dev/null +++ b/script.js @@ -0,0 +1,57 @@ +let currentInput = ''; +let previousInput = ''; +let operation = null; + +function appendNumber(number) { + currentInput += number.toString(); + document.getElementById('display').value = currentInput; +} + +function appendDecimal(dot) { + if (!currentInput.includes(dot)) { + currentInput += dot; + document.getElementById('display').value = currentInput; + } +} + +function chooseOperation(op) { + if (currentInput !== '') { + previousInput = currentInput; + operation = op; + currentInput = ''; + } +} + +function calculateResult(equals) { + let result; + const prev = parseFloat(previousInput); + const curr = parseFloat(currentInput); + if (isNaN(prev) || isNaN(curr)) return; + switch (operation) { + case '+': + result = prev + curr; + break; + case '-': + result = prev - curr; + break; + case '*': + result = prev * curr; + break; + case '/': + result = prev / curr; + break; + default: + return; + } + currentInput = result.toString(); + document.getElementById('display').value = currentInput; + previousInput = ''; + operation = null; +} + +function clearDisplay() { + currentInput = ''; + previousInput = ''; + operation = null; + document.getElementById('display').value = ''; +}s \ No newline at end of file