-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
83 lines (73 loc) · 1.87 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
function add(num1,num2,text){
num1=parseInt(num1);
num2=parseInt(num2);
ans=num1+num2;
text.value=ans;
}
function subtract (num1,num2,text){
num1=parseInt(num1);
num2=parseInt(num2);
ans=num1-num2;
text.value=ans
}
function multiply (num1,num2,text){
num1=parseInt(num1);
num2=parseInt(num2);
ans=num1*num2;
text.value=ans
}
function divide (num1,num2,text){
num1=parseInt(num1);
num2=parseInt(num2);
ans=num1/num2;
text.value=ans
}
function operator(num1,sign,num2,text){
switch(sign){
case "+":
add(num1,num2,text);
break;
case "-":
subtract(num1,num2,text);
break;
case "*":
multiply(num1,num2,text);
break;
case "/":
divide(num1,num2,text);
break;
}
}
function main(){
const text=document.querySelector(".screen");
const clear=document.querySelector("#clear");
const equals=document.querySelector("#equals");
const sign=document.querySelectorAll(".sign");
const numbers=document.querySelectorAll(".number")
clear.addEventListener("click",()=>{
text.value=""
})
for (let i =0;i<numbers.length;i++){
numbers[i].addEventListener("click",()=>{
text.value+=numbers[i].innerHTML;
})
}
for (let i =0;i<sign.length;i++){
sign[i].addEventListener("click",()=>{
val=text.value.split(" ");
if (val.length==1){
num1=text.value;
text.value+=" "+sign[i].innerHTML+" ";
}
else{
operator(val[0],val[1],val[2],text);
text.value+=" "+sign[i].innerHTML+" ";
}
})
}
equals.addEventListener("click",()=>{
val=text.value.split(" ");
operator(val[0],val[1],val[2],text);
})
}
main()