-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.ts
More file actions
150 lines (132 loc) · 3.87 KB
/
Copy pathscript.ts
File metadata and controls
150 lines (132 loc) · 3.87 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
146
147
148
149
150
type Operator = '+' | '-' | '*' | '/';
type ButtonValue = string;
interface CalculatorState {
INITIAL_VALUE: string;
ERROR_MESSAGE: string;
}
const display = document.getElementById("display-value") as HTMLElement | null;
const buttons = document.querySelectorAll<HTMLButtonElement>(".btn");
if (!display) {
throw new Error("Display element not found");
}
const CALCULATOR_STATE: CalculatorState = {
INITIAL_VALUE: "0",
ERROR_MESSAGE: "Error"
};
let operator: Operator | null = null;
let waitingForSecondValue = false;
let currentValue: string = CALCULATOR_STATE.INITIAL_VALUE;
let firstValue: number | null = null;
let isResult = false;
function updateDisplay():void {
display!.textContent = currentValue;
}
updateDisplay();
buttons.forEach(button => {
button.addEventListener('click', (e: MouseEvent) => {
const target = e.currentTarget as HTMLButtonElement;
const value = target.dataset.value;
if (value) {
handleButtonClick(value);
}
});
});
document.addEventListener('keydown', (e: KeyboardEvent) => {
const keyMap: Record<string, ButtonValue> = {
'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: ButtonValue): void {
if (isNumber(value)) {
handleNumber(value);
}
else if (isOperator(value)) {
handleOperator(value);
}
else if (value === '=') {
handleEqual();
}
else if (value === 'C') {
resetCalculator();
}
updateDisplay();
}
function isNumber(val: string): boolean {
if (isResult) {
resetCalculator();
}
return val.trim() !== '' && !Number.isNaN(Number(val));
}
function isOperator(val: string): val is Operator{
return ['+', '-', '*', '/'].includes(val);
}
function handleNumber(num: string): void{
if (waitingForSecondValue) {
currentValue = num;
waitingForSecondValue = false;
} else {
currentValue = currentValue === '0' ? num : currentValue + num;
}
}
function calculate(
first: number,
operator: Operator,
second: number
): number | string {
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: Operator):void {
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 as number;
}
waitingForSecondValue = true;
operator = nextOperator;
isResult = false;
}
function handleEqual():void {
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(): void {
currentValue = '0';
firstValue = null;
operator = null;
waitingForSecondValue = false;
isResult = false;
}