calculator

Spread the love






Calculator


0













function appendNumber(number) { if (currentOperand === '0' || shouldResetScreen) { currentOperand = number; shouldResetScreen = false; } else { currentOperand += number; } updateDisplay(); }

function appendDecimal() { if (shouldResetScreen) { currentOperand = '0'; shouldResetScreen = false; } if (!currentOperand.includes('.')) { currentOperand += '.'; } updateDisplay(); }

function appendOperator(op) { if (operator !== null) calculate(); operator = op; previousOperand = currentOperand + ' ' + op; shouldResetScreen = true; updateDisplay(); }

function calculate() { if (operator === null || shouldResetScreen) return;

const prev = parseFloat(previousOperand); const current = parseFloat(currentOperand); let computation;

switch (operator) { case '+': computation = prev + current; break; case '-': computation = prev - current; break; case '*': computation = prev * current; break; case '/': computation = prev / current; break; default: return; }

currentOperand = computation.toString(); operator = null; previousOperand = ''; shouldResetScreen = true; updateDisplay(); }

function clearDisplay() { currentOperand = '0'; previousOperand = ''; operator = null; shouldResetScreen = false; updateDisplay(); }

// Handle keyboard input document.addEventListener('keydown', (event) => { if (event.key >= '0' && event.key <= '9') appendNumber(event.key); if (event.key === '.') appendDecimal(); if (event.key === '=' || event.key === 'Enter') calculate(); if (event.key === 'Escape') clearDisplay(); if (event.key === '+' || event.key === '-' || event.key === '*' || event.key === '/') { appendOperator(event.key); } });


Solverwp- WordPress Theme and Plugin

Social Media Auto Publish Powered By : XYZScripts.com