-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
125 lines (109 loc) · 3.12 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
let firstNum, secondNum, op, display, calc,dec;
const screen = document.querySelector(".screen");
calc=0;
display = "0";
firstNum = "";
secondNum = "";
dec=0;
screen.textContent = display;
const ac = document.querySelector("#ac");
ac.addEventListener("mouseover",()=>{
ac.classList.add("select");
});
ac.addEventListener("mouseleave",()=>{
ac.classList.remove("select");
});
ac.addEventListener("click",()=>
{
display = "";
screen.textContent = display;
calc=0; firstNum=0; secondNum=0;
});
const ce = document.querySelector("#ce");
ce.addEventListener("mouseover",()=>{
ce.classList.add("select");
});
ce.addEventListener("mouseleave",()=>{
ce.classList.remove("select");
});
ce.addEventListener("click",()=>
{
display = display.toString();
display = display.slice(0,-1);
if(!display) {display = "0"; calc=0; firstNum=0; secondNum=0;}
screen.textContent = display;
});
function add(a,b){
let ans = a+b;
if (Math.floor(ans)!=ans && ans.toString().split(".")[1].length >= 8)
return ans.toFixed(8)
return ans;
}
function sub(a,b){
let ans = a-b;
if (Math.floor(ans)!=ans && ans.toString().split(".")[1].length >= 8)
return ans.toFixed(8)
return ans;
}
function mul(a,b){
let ans = a*b;
if (Math.floor(ans)!=ans && ans.toString().split(".")[1].length >= 8)
return ans.toFixed(8)
return ans;
}
function divide(a,b){
let ans = a/b;
if (Math.floor(ans)!=ans && ans.toString().split(".")[1].length >= 8)
return ans.toFixed(8)
return ans;
}
function operate(first,second,op){
first = +first;
second = + second;
if(op==="+") return add(first,second);
else if(op==="-") return sub(first,second);
else if(op==="*") return mul(first,second);
else if(op==="/") return divide(first,second);
}
let nums = document.querySelectorAll(".number");
nums = Array.from(nums);
for(let i=0;i<nums.length;i++){
nums[i].addEventListener("click",()=>
{
if(nums[i].textContent==="=") {calc=0; calculate(display);}
else if(nums[i].textContent === "+"||nums[i].textContent==="-"||nums[i].textContent==="*"||nums[i].textContent==="/")
{
display= display+" "+ nums[i].textContent + " ";
calc++;
dec=0;
}
else if(nums[i].textContent==="."){dec++; display+=nums[i].textContent;}
else display+=nums[i].textContent;
if(calc > 1) {calc = 0; calculate(display);}
if(dec>1){dec = 1; display=display.slice(0,-1);}
if(display[0]==="0" && calc===0) display=display.slice(1);
screen.textContent = display;
});
nums[i].addEventListener("mouseover",()=>{
nums[i].classList.add("select");
});
nums[i].addEventListener("mouseleave",()=>{
nums[i].classList.remove("select");
});
}
function calculate(s)
{
let arr = s.split(" ");
if(!arr[2]) return;
firstNum = arr[0];
op = arr[1];
secondNum = arr[2];
display = operate (firstNum,secondNum,op);
if(arr[3])
{
op = arr[3];
display = display + " " + op + " ";
calc = 1;
}
screen.textContent = display;
}