-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
126 lines (126 loc) · 3.39 KB
/
Copy pathscript.js
File metadata and controls
126 lines (126 loc) · 3.39 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
"use strict";
const display = document.getElementById("display-value");
const buttons = document.querySelectorAll(".btn");
if (!display) {
throw new Error("Display element not found");
}
const CALCULATOR_STATE = {
INITIAL_VALUE: "0",
ERROR_MESSAGE: "Error"
};
let operator = null;
let waitingForSecondValue = false;
let currentValue = CALCULATOR_STATE.INITIAL_VALUE;
let firstValue = null;
let isResult = false;
function updateDisplay() {
display.textContent = currentValue;
}
updateDisplay();
buttons.forEach(button => {
button.addEventListener('click', (e) => {
const target = e.currentTarget;
const value = target.dataset.value;
if (value) {
handleButtonClick(value);
}
});
});
document.addEventListener('keydown', (e) => {
const keyMap = {
'0': '0', '1': '1', '2': '2', '3': '3', '4': '4',
'5': '5', '6': '6', '7': '7', '8': '8', '9': '9',
'+': '+', '-': '-', '*': '*', '/': '/',
'Enter': '=', '=': '=',
'Escape': 'C', 'c': 'C', 'C': 'C'
};
const value = keyMap[e.key];
if (value) {
e.preventDefault();
handleButtonClick(value);
}
});
function handleButtonClick(value) {
if (isNumber(value)) {
handleNumber(value);
}
else if (isOperator(value)) {
handleOperator(value);
}
else if (value === '=') {
handleEqual();
}
else if (value === 'C') {
resetCalculator();
}
updateDisplay();
}
function isNumber(val) {
if (isResult) {
resetCalculator();
}
return val.trim() !== '' && !Number.isNaN(Number(val));
}
function isOperator(val) {
return ['+', '-', '*', '/'].includes(val);
}
function handleNumber(num) {
if (waitingForSecondValue) {
currentValue = num;
waitingForSecondValue = false;
}
else {
currentValue = currentValue === '0' ? num : currentValue + num;
}
}
function calculate(first, operator, second) {
switch (operator) {
case '+': return first + second;
case '-': return first - second;
case '*': return first * second;
case '/': return second !== 0 ? first / second : CALCULATOR_STATE.ERROR_MESSAGE;
default: return second;
}
}
function handleOperator(nextOperator) {
const value = parseFloat(currentValue);
if (operator && waitingForSecondValue) {
operator = nextOperator;
return;
}
if (firstValue === null) {
firstValue = value;
}
else if (operator && !isResult) {
const result = calculate(firstValue, operator, value);
if (result === CALCULATOR_STATE.ERROR_MESSAGE) {
currentValue = result;
resetCalculator();
return;
}
currentValue = String(result);
firstValue = result;
}
waitingForSecondValue = true;
operator = nextOperator;
isResult = false;
}
function handleEqual() {
const value = parseFloat(currentValue);
if (operator && firstValue !== null) {
const result = calculate(firstValue, operator, value);
currentValue = String(result);
firstValue = typeof result === 'number' ? result : null;
;
operator = null;
waitingForSecondValue = false;
isResult = true;
}
}
function resetCalculator() {
currentValue = '0';
firstValue = null;
operator = null;
waitingForSecondValue = false;
isResult = false;
}