-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
143 lines (125 loc) · 3.27 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
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
const resultScreen = document.querySelector(".screen");
const btnClear = document.querySelector(".clear-key");
const btnNum = document.querySelectorAll(".num-key");
const btnOp = document.querySelectorAll(".op-key");
const btnPoint = document.querySelector(".point-key");
const btnResult = document.querySelector(".result-key");
const btnDel = document.querySelector(".del-key");
let op = "";
let num1 = "";
let num2 = "";
let result = "";
function add(num1, num2) {
return Number(num1) + Number(num2);
}
function subtract(num1, num2) {
return Number(num1) - Number(num2);
}
function multiply(num1, num2) {
return Number(num1) * Number(num2);
}
function divide(num1, num2) {
return Number(num1) / Number(num2);
}
function modulo(num1, num2) {
return Number(num1) % Number(num2);
}
function operate() {
if (op === '+') {
return add(num1, num2);
} else if (op === '-') {
return subtract(num1, num2);
} else if (op === '*') {
return multiply(num1, num2);
} else if (op === '/') {
return divide(num1, num2);
} else if (op === '%') {
return modulo(num1, num2);
}
}
function display(num) {
if (num == "." && num1.includes(".") && op == "") {
return;
} else if (num == "." && num1 === "") {
num1 = "0."
resultScreen.innerHTML = num1;
} else if (num1 == "") {
num1 = num;
resultScreen.innerHTML = num1;
} else if (op == "") {
num1 = num1 + num;
resultScreen.innerHTML = num1;
}
if (num == "." && num2.includes(".") && op != "") {
return;
} else if (num == "." && num2 == "" && op != "") {
num2 = "0."
resultScreen.innerHTML = num2;
} else if (num1 != "" && num2 == "" && op != "") {
num2 = num;
resultScreen.innerHTML = num2;
} else if (op != "") {
num2 = num2 + num;
resultScreen.innerHTML = num2;
}
}
btnDel.addEventListener("click", function () {
let tempNum;
if (op === "") {
tempNum = num1.slice(0, -1);
num1 = tempNum;
if (num1 == "") {
resultScreen.textContent = "0";
} else {
resultScreen.textContent = num1;
}
} else {
tempNum = num2.slice(0, -1);
num2 = tempNum;
if (num2 == "") {
resultScreen.textContent = "0";
} else {
resultScreen.textContent = num2;
}
}
})
function clearScreen() {
num1 = "";
num2 = "";
op = "";
result = "";
resultScreen.innerHTML = '0';
}
function calcResult() {
result = operate();
let temp;
if ((result % 2) === 0) {
temp = result;
} else {
temp = result.toFixed(3);
temp = parseFloat(temp);
}
clearScreen();
num1 = temp;
result = temp;
resultScreen.innerHTML = result;
}
btnNum.forEach(number => {
number.addEventListener("click", event => {
const num = number.innerHTML;
display(num);
});
});
btnOp.forEach(operator => {
operator.addEventListener("click", event => {
if(num1 != "" && op == "") {
op = operator.innerHTML;
}
});
});
btnClear.addEventListener("click", event => {
clearScreen();
});
btnResult.addEventListener("click", event => {
calcResult();
});