forked from itsecd/web-base
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
145 lines (119 loc) · 5.08 KB
/
script.js
File metadata and controls
145 lines (119 loc) · 5.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
document.addEventListener('DOMContentLoaded', function() {
const number1Input = document.getElementById('number1');
const number2Input = document.getElementById('number2');
const operationSelect = document.getElementById('operation');
const calculateButton = document.getElementById('calculate');
const resultElement = document.getElementById('result');
const error1Element = document.getElementById('error1');
const error2Element = document.getElementById('error2');
const historyList = document.getElementById('history-list');
let operationsHistory = [];
function updateHistory() {
historyList.innerHTML = '';
if (operationsHistory.length === 0) {
historyList.innerHTML = '<div class="history-empty">Transaction history is empty</div>';
return;
}
const recentHistory = operationsHistory.slice(-5).reverse();
recentHistory.forEach(item => {
const historyItem = document.createElement('div');
historyItem.className = 'history-item';
historyItem.textContent = item;
historyList.appendChild(historyItem);
});
}
function addToHistory(num1, num2, operation, result) {
let operationSymbol;
switch(operation) {
case 'add': operationSymbol = '+'; break;
case 'subtract': operationSymbol = '-'; break;
case 'multiply': operationSymbol = '×'; break;
case 'divide': operationSymbol = '÷'; break;
default: operationSymbol = '?';
}
const historyEntry = `${num1} ${operationSymbol} ${num2} = ${result}`;
operationsHistory.push(historyEntry);
updateHistory();
}
function validateInput() {
let isValid = true;
error1Element.textContent = '';
error2Element.textContent = '';
if (number1Input.value === '') {
error1Element.textContent = 'Please enter the first number';
isValid = false;
} else if (isNaN(parseFloat(number1Input.value))) {
error1Element.textContent = 'Please enter a valid number';
isValid = false;
}
if (number2Input.value === '') {
error2Element.textContent = 'Please enter the second number';
isValid = false;
} else if (isNaN(parseFloat(number2Input.value))) {
error2Element.textContent = 'Please enter a valid number';
isValid = false;
}
if (operationSelect.value === 'divide' && parseFloat(number2Input.value) === 0) {
error2Element.textContent = 'Division by zero is impossible';
isValid = false;
}
return isValid;
}
function calculate() {
if (!validateInput()) {
resultElement.textContent = '—';
resultElement.style.color = '#ff0000ff';
return;
}
const num1 = parseFloat(number1Input.value);
const num2 = parseFloat(number2Input.value);
const operation = operationSelect.value;
let result;
switch(operation) {
case 'add':
result = num1 + num2;
break;
case 'subtract':
result = num1 - num2;
break;
case 'multiply':
result = num1 * num2;
break;
case 'divide':
result = num1 / num2;
break;
default:
result = NaN;
}
if (isNaN(result)) {
resultElement.textContent = 'Calculation error';
resultElement.style.color = '#e74c3c';
} else if (!isFinite(result)) {
resultElement.textContent = 'Infinity';
resultElement.style.color = '#e74c3c';
} else {
const formattedResult = Math.abs(result) > 1e10 || (Math.abs(result) < 1e-10 && result !== 0)
? result.toExponential(5)
: parseFloat(result.toFixed(10)).toString();
resultElement.textContent = formattedResult;
resultElement.style.color = '#ff0000ff';
addToHistory(num1, num2, operation, formattedResult);
}
}
calculateButton.addEventListener('click', calculate);
number1Input.addEventListener('keypress', function(e) {
if (e.key === 'Enter') calculate();
});
number2Input.addEventListener('keypress', function(e) {
if (e.key === 'Enter') calculate();
});
operationSelect.addEventListener('change', function() {
if (this.value === 'divide' && parseFloat(number2Input.value) === 0) {
error2Element.textContent = 'Деление на ноль невозможно';
} else {
error2Element.textContent = '';
}
});
updateHistory();
number1Input.focus();
});