-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
46 lines (39 loc) · 1007 Bytes
/
script.js
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
const display = document.getElementById('display');
function appendNumber(number) {
if (display.value === "0") {
display.value = number;
} else {
display.value += number;
}
}
function appendOperator(operator) {
if (operator === "x") {
display.value += ` x `;
} else {
display.value += operator;
}
}
function deleteNumber() {
display.value = display.value.slice(0, -1);
if (display.value === '') {
display.value = '0';
}
}
function resetDisplay() {
display.value = "0";
}
function calculate() {
try {
// Replace "x" with "*"
let expression = display.value.replace(/x/g, "*");
// Evaluate the expression
let result = eval(expression);
if (Number.isInteger(result)) {
display.value = result;
} else {
display.value = result.toFixed(3); // 3 decimal places
}
} catch (error) {
display.value = "Error";
}
}