-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
67 lines (59 loc) · 2.08 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
let numsArr = document.getElementById("nums").children;
let act = document.getElementById("actions").children;
let upperScrn = document.getElementById("upper");
let lowerScrn = document.getElementById("lower");
let sideScrn = document.getElementById("sidescrn");
let rslt = act[4];
let start = 1;
for (let i = 3; i < 14; i++) {
numsArr[i].setAttribute("onclick", "printNum(this)");
}
function printNum(elem) {
// this.style //add some style
if (start) {
for (let i = 0; i < 4; i++) {
act[i].setAttribute("onclick", "oprtor(this)");
}
rslt.setAttribute("onclick", "res(this)");
numsArr[12].setAttribute("onclick", "printNum(this)");
upperScrn.innerText = "";
lowerScrn.innerText = "";
sideScrn.innerText = "";
start = 0;
} else if (sideScrn.innerText != "") {
for (let i = 0; i < 4; i++) {
act[i].onclick = "";
}
}
lowerScrn.innerText += elem.innerText;
if (elem.innerText == '.') elem.onclick = "";
}
function oprtor(elem) {
sideScrn.innerText = elem.innerText;
upperScrn.innerText += lowerScrn.innerText;
lowerScrn.innerText = "";
numsArr[12].setAttribute("onclick", "printNum(this)");
}
function res(elem) {
elem.onclick = "";
for (let i = 0; i < 4; i++) {
act[i].onclick = "";
}
upperScrn.innerText += sideScrn.innerText + lowerScrn.innerText + elem.innerText;
sideScrn.innerText = elem.innerText;
lowerScrn.innerText = cal();
upperScrn.innerText += lowerScrn.innerText;
numsArr[12].setAttribute("onclick", "printNum(this)");
start = 1;
}
function cal() {
let str = upperScrn.innerText;
if (str.includes('+'))
return ((str.split('+')[0] - 0) + (str.slice(0, -1).split('+')[1] - 0));
if (str.includes('-'))
return ((str.split('-')[0] - 0) - (str.slice(0, -1).split('-')[1] - 0));;
if (str.includes('*'))
return ((str.split('*')[0] - 0) * (str.slice(0, -1).split('*')[1] - 0));;
if (str.includes('/'))
return ((str.split('/')[0] - 0) / (str.slice(0, -1).split('/')[1] - 0));;
}