-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
47 lines (42 loc) · 1.11 KB
/
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
47
let currentInput = "";
let isResultShown = false;
function appendToDisplay(value) {
if (isResultShown) {
document.getElementById("display").value = "";
isResultShown = false;
}
currentInput += value;
document.getElementById("display").value = currentInput;
}
function clearDisplay() {
currentInput = "";
document.getElementById("display").value = "";
}
function calcResult() {
try {
const result = eval(currentInput);
document.getElementById("display").value = result;
isResultShown = true;
currentInput = result.toString();
} catch (error) {
document.getElementById("display").value = "Error";
}
}
function deleteLast() {
if (isResultShown) {
clearDisplay();
} else {
currentInput = currentInput.slice(0, -1);
document.getElementById("display").value = currentInput;
}
}
function calcPercentage() {
try {
const percentage = eval(currentInput) / 100;
document.getElementById("display").value = percentage;
isResultShown = true;
currentInput = percentage.toString();
} catch (error) {
document.getElementById("display").value = "Error";
}
}