/
MashkoA
/
web-nodejs
Обзор
Документация
Войти
/
MashkoA
/
web-nodejs
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
main
public/lab7/task1/script.js
74 строки
3 KB
umarovazizjohn
l7
09 апр 2026, 10:12
09 апр 2026, 10:12
48a32d1
Код
Авторство
О чём код?
'use strict'; document.addEventListener('DOMContentLoaded', function() { const operand1Input = document.getElementById('operand1'); const operand2Input = document.getElementById('operand2'); const operatorSelect = document.getElementById('operator'); const doCalcButton = document.getElementById('doCalc'); const resultSpan = document.getElementById('result'); const processlabel = document.getElementById('processlabel'); doCalcButton.addEventListener('click', async function() { resultSpan.textContent = ''; const operand1 = operand1Input.value.trim(); const operand2 = operand2Input.value.trim(); const operator = operatorSelect.value; // проверка на пустоту if (!operand1 || !operand2) { processlabel.textContent = 'Оба операнда должны быть введены'; return; } // проверка на корректность значений const num1 = parseFloat(operand1); const num2 = parseFloat(operand2); if (isNaN(num1) || isNaN(num2)) { processlabel.textContent = 'Введите корректные числа'; return; } // визуал processlabel.textContent = 'Вычисление...'; try { // операция let operation; switch (operator) { case '+': operation = 'add'; break; case '-': operation = 'subtract'; break; case '*': operation = 'multiply'; break; case '/': operation = 'divide'; break; default: operation = operator; } // формирование запроса const response = await fetch(`/api/lab7/task1/calc?operation=${operation}&a=${num1}&b=${num2}`); const data = await response.json(); if (!response.ok) { throw new Error(data.error || 'Ошибка сервера'); } // какой-то результат console.textContent = data.expression; resultSpan.textContent = data.result; processlabel.textContent = ''; } catch (error) { // ошибка processlabel.textContent = `Ошибка: ${error.message}`; } }); // enter [operand1Input, operand2Input].forEach(input => { input.addEventListener('keypress', function(event) { if (event.key === 'Enter') { doCalcButton.click(); } }); }); });