-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
132 lines (112 loc) · 2.99 KB
/
script.js
File metadata and controls
132 lines (112 loc) · 2.99 KB
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
let btn1 = document.querySelector("#btn1")
let btn2 = document.querySelector("#btn2")
let btn3 = document.querySelector("#btn3")
let btn4 = document.querySelector("#btn4")
let btn5 = document.querySelector("#btn5")
let btn6 = document.querySelector("#btn6")
let btn7 = document.querySelector("#btn7")
let btn8 = document.querySelector("#btn8")
let btn9 = document.querySelector("#btn9")
let btnplus = document.querySelector("#btnplus")
let btnminus = document.querySelector("#btnminus")
let btndivide = document.querySelector("#btndivide")
let btntimes = document.querySelector("#btntimes")
let btnclear = document.querySelector("#btnclear")
let btnequal = document.querySelector("#btnequal")
let display = document.querySelector(".display")
let operand = ""
let num1 = ""
let num2 = ""
display.textContent = ""
btn1.addEventListener('click',function() {
setNum("1")
});
btn2.addEventListener('click',function() {
setNum("2")
});
btn3.addEventListener('click',function() {
setNum("3")
});
btn4.addEventListener('click',function() {
setNum("4")
});
btn5.addEventListener('click',function() {
setNum("5")
});
btn6.addEventListener('click',function() {
setNum("6")
});
btn7.addEventListener('click',function() {
setNum("7")
});
btn8.addEventListener('click',function() {
setNum("8")
});
btn9.addEventListener('click',function() {
setNum("9")
});
btnminus.addEventListener('click',function() {
if (operand == ""){
display.textContent += "-"
operand = "-"
}
});
btnplus.addEventListener('click',function() {
if (operand == ""){
display.textContent += "+"
operand = "+"
}
});
btntimes.addEventListener('click',function() {
if (operand == ""){
display.textContent += "*"
operand = "*"
}
});
btndivide.addEventListener('click',function() {
if (operand == ""){
display.textContent += "/"
operand = "/"
}
});
btnclear.addEventListener('click',function() {
num1 = ""
num2 = ""
operand = "";
display.textContent = ""
});
btnequal.addEventListener('click',function() {
num1 = operate(num1,operand,num2).toString()
num2 = "";
operand = "";
});
function setNum(num){
if(operand == ""){
num1 += num
display.textContent += num
}else{
num2 += num
display.textContent += num
}
}
function operate(num1,operand,num2){
if(operand == "+"){
display.textContent = +num1 + +num2;
return +num1 + +num2;
}else if(operand == "-"){
display.textContent = +num1 - +num2;
return +num1 - +num2;
}else if(operand == "*"){
display.textContent = +num1 * +num2;
return +num1 * +num2;
}else if(operand == "/"){
if(num2 == 0){
display.textContent = "undefined"
}else{
display.textContent = Math.round((+num1 / +num2) * 10000) /10000
return Math.round((+num1 / +num2) * 10000) /10000
}
}else{
alert("Operand was undefined")
}
}