-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
172 lines (153 loc) · 4.42 KB
/
main.js
File metadata and controls
172 lines (153 loc) · 4.42 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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
//Selecting display area
const display = document.querySelector('.displayArea');
//Selecting buttons
const buttons = document.querySelectorAll('.btn-dark');
const plusButton = document.querySelector('.plusBtn');
const minusButton = document.querySelector('.minusBtn');
const equalsButton = document.querySelector('.equalsBtn');
const multiplyButton = document.querySelector('.multiplyBtn');
const dividerButton = document.querySelector('.dividerBtn');
const clearButton = document.querySelector('.clearBtn');
const undoButton = document.querySelector('.minusPlusBtn');
const remainderButton = document.querySelector('.remainderBtn');
//Initial values
let currentCalculation = '';
let currentResult = 0;
let sign = '';
let newOperation = true;
let decimalEntered = false;
buttons.forEach((button) => {
button.addEventListener('click', () => {
const buttonValue = button.innerHTML;
if (currentCalculation.length < 10) {
// Check if not exceeding 10 digits
if (buttonValue === '.') {
if (!decimalEntered) {
currentCalculation += buttonValue;
decimalEntered = true;
}
} else {
if (currentCalculation === '0' && buttonValue === '0') {
// Prevent adding more zeros at the beginning 'thanks for Viktor project'
return;
}
if (currentCalculation === '0') {
// Remove the leading zero if it's followed by a non-zero digit
currentCalculation = '';
}
currentCalculation += buttonValue;
}
display.innerHTML = currentCalculation;
}
});
});
//addition
plusButton.addEventListener('click', () => {
operate();
sign = '+';
newOperation = true;
decimalEntered = false;
});
//Substraction
minusButton.addEventListener('click', () => {
operate();
sign = '-';
newOperation = true;
});
//Multiplication
multiplyButton.addEventListener('click', () => {
operate();
sign = '*';
newOperation = true;
});
//Division
dividerButton.addEventListener('click', () => {
operate();
sign = '/';
newOperation = true;
});
remainderButton.addEventListener('click', () => {
operate();
sign = '%';
newOperation = true;
});
equalsButton.addEventListener('click', () => {
operate();
sign = '';
newOperation = true;
});
clearButton.addEventListener('click', () => {
currentCalculation = '';
currentResult = 0;
sign = '';
newOperation = true;
decimalEntered = false; // Reset the flag
display.innerHTML = '0';
});
undoButton.addEventListener('click', () => {
if (currentCalculation.length > 0) {
if (currentCalculation[currentCalculation.length - 1] === '.') {
decimalEntered = false;
}
currentCalculation = currentCalculation.slice(0, -1);
display.innerHTML = currentCalculation;
}
});
//This function must take num1,num2 and operation!!!!!
function operate() {
if (currentCalculation !== '') {
const currentValue = parseFloat(currentCalculation);
if (sign === '+') {
currentResult += currentValue;
} else if (sign === '-') {
currentResult -= currentValue;
} else if (sign === '*') {
currentResult *= currentValue;
} else if (sign === '%') {
currentResult = (currentValue / 100) * currentResult;
currentResult = parseFloat(currentResult.toFixed(2));
} else if (sign === '/') {
if (currentValue === 0) {
display.innerHTML = 'error';
return;
}
currentResult /= currentValue;
currentResult = parseFloat(currentResult.toFixed(2));
} else {
currentResult = currentValue;
}
// Truncate result to 10 digits
currentResult = parseFloat(currentResult.toFixed(10));
display.innerHTML = currentResult;
currentCalculation = '';
decimalEntered = false;
}
}
// Add keyboard link to the calculator
document.addEventListener('keydown', (event) => {
const key = event.key;
// Check if the pressed key is a number or operator key
if (
!isNaN(key) ||
key === '+' ||
key === '-' ||
key === '*' ||
key === '/' ||
key === '%' ||
key === '=' ||
key === 'Enter' ||
key === '.'
) {
// Simulate a click on the corresponding button
const button = document.querySelector(`.keyboard[data-key="${key}"]`);
if (button) {
button.click();
}
} else if (key === 'Backspace') {
// Simulate a click on the undo button
undoButton.click();
} else if (key === 'Escape') {
// Simulate a click on the clear button
clearButton.click();
}
});