Changed around line 1
+ document.addEventListener('DOMContentLoaded', () => {
+ const display = document.getElementById('result');
+ let currentValue = '';
+ let previousValue = '';
+ let operation = null;
+ let shouldResetDisplay = false;
+
+ document.querySelectorAll('.pentagon-btn').forEach(button => {
+ button.addEventListener('click', () => {
+ const value = button.textContent;
+
+ if (value >= '0' && value <= '9' || value === '.') {
+ if (shouldResetDisplay) {
+ currentValue = '';
+ shouldResetDisplay = false;
+ }
+ if (value === '.' && currentValue.includes('.')) return;
+ currentValue += value;
+ updateDisplay();
+ } else if ('+-×÷'.includes(value)) {
+ if (currentValue !== '') {
+ if (previousValue !== '') {
+ calculate();
+ }
+ previousValue = currentValue;
+ operation = value;
+ shouldResetDisplay = true;
+ }
+ } else if (value === '=') {
+ calculate();
+ } else if (value === 'C') {
+ clear();
+ }
+ });
+ });
+
+ function updateDisplay() {
+ display.value = currentValue;
+ }
+
+ function calculate() {
+ if (previousValue === '' || currentValue === '') return;
+
+ const prev = parseFloat(previousValue);
+ const curr = parseFloat(currentValue);
+
+ switch(operation) {
+ case '+':
+ currentValue = (prev + curr).toString();
+ break;
+ case '-':
+ currentValue = (prev - curr).toString();
+ break;
+ case '×':
+ currentValue = (prev * curr).toString();
+ break;
+ case '÷':
+ if (curr === 0) {
+ currentValue = 'Error';
+ } else {
+ currentValue = (prev / curr).toString();
+ }
+ break;
+ }
+
+ operation = null;
+ previousValue = '';
+ updateDisplay();
+ shouldResetDisplay = true;
+ }
+
+ function clear() {
+ currentValue = '';
+ previousValue = '';
+ operation = null;
+ updateDisplay();
+ }
+ });