/
kosalexs
/
test
Обзор
Документация
Войти
/
kosalexs
/
test
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
main
script.js
97 строк
2 KB
Konstantin Alekseev
Add square function to calculator
10 фев 2026, 15:09
10 фев 2026, 15:09
f1e262c
Код
Авторство
О чём код?
let currentInput = ''; let operator = null; let previousInput = ''; let resetScreen = false; function appendToDisplay(value) { const display = document.getElementById('display'); if (resetScreen) { display.value = ''; resetScreen = false; } display.value += value; currentInput = display.value; } function setOperation(op) { if (currentInput === '') return; if (previousInput !== '') { calculateResult(); } operator = op; previousInput = currentInput; currentInput = ''; resetScreen = true; } function calculateResult() { if (operator === null || currentInput === '') return; const display = document.getElementById('display'); const prev = parseFloat(previousInput); const current = parseFloat(currentInput); let result = 0; switch (operator) { case '+': result = prev + current; break; case '-': result = prev - current; break; case '*': result = prev * current; break; case '/': if (current === 0) { alert('Деление на ноль!'); clearDisplay(); return; } result = prev / current; break; } display.value = result; currentInput = result; operator = null; previousInput = ''; resetScreen = true; } function roundToInteger() { const display = document.getElementById('display'); if (display.value === '') return; const value = parseFloat(display.value); display.value = Math.round(value); currentInput = display.value; } function roundToTwoDecimals() { const display = document.getElementById('display'); if (display.value === '') return; const value = parseFloat(display.value); display.value = Math.round(value * 100) / 100; currentInput = display.value; } function clearDisplay() { document.getElementById('display').value = ''; currentInput = ''; previousInput = ''; operator = null; resetScreen = false; } function deleteLastChar() { const display = document.getElementById('display'); display.value = display.value.slice(0, -1); currentInput = display.value; } function squareNumber() { const display = document.getElementById('display'); if (display.value === '') return; const value = parseFloat(display.value); display.value = value * value; currentInput = display.value; }