forked from LoneLombax1/Calculator1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Calculator.js
64 lines (51 loc) · 1.15 KB
/
Calculator.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
let oper;
let var1;
let var2;
let op1;
let done;
function cleared() {
let screen1 = document.getElementById("screen");
screen1.innerText = "";
oper = false;
op1 = "";
done = false;
}
function number(num) {
if (done) cleared();
let buttn = document.getElementById("screen");
if (buttn.innerHTML === "Screen") {
buttn.innerHTML = "";
}
buttn.style.padding = "33px";
buttn.innerHTML += num;
}
function operator1(op) {
var1 = document.getElementById("screen").innerHTML;
cleared();
op1 = op;
oper = true;
}
function decim() {
if (done) cleared();
const screen = document.getElementById("screen");
if (screen.innerHTML.includes(".")) {
screen.innerHTML = "ERROR!";
return;
}
if (screen.innerHTML === "") {
screen.innerHTML = "0.";
} else {
inputNumber(".");
}
}
function calculate() {
if (!oper) return; // Prevent calculation if no operator set
let result;
if (operator === "^") {
result = Math.pow(firstOperand, secondOperand);
} else {
result = eval(`${+firstOperand} ${operator} ${+secondOperand}`);
}
document.getElementById("screen").innerHTML = result;
calculationDone = true;
}