-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode.js
More file actions
181 lines (161 loc) · 5.02 KB
/
code.js
File metadata and controls
181 lines (161 loc) · 5.02 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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
class calculadora {
constructor(estado) {
this.encendida = estado;
}
sumar(a = 0, b = 0) {
if (this.encendida == true) {
const c = parseInt(a) + parseInt(b);
return c;
}
}
restar(a = 0, b = 0) {
if (this.encendida == true) {
const c = parseInt(a) - parseInt(b);
return c;
}
}
dividir(a = 0, b = 0) {
if (this.encendida == true) {
const c = parseInt(a) / parseInt(b);
return c;
}
}
multiplicar(a = 0, b = 0) {
if (this.encendida == true) {
const c = parseInt(a) * parseInt(b);
return c;
}
}
}
const calc = new calculadora(true);
var primerValor = document.querySelector(`#primerValor`);
var segundoValor = document.querySelector(`#segundoValor`);
var operacion = document.querySelector(`#cajaOperacion`);
var cajaResultado = document.querySelector(`#resultado`);
const opSumar = document.querySelector(`#sumar`);
const opRestar = document.querySelector(`#restar`);
const opMultiplicar = document.querySelector(`#multiplicar`);
const opDividir = document.querySelector(`#dividir`);
const opBorrar = document.querySelector(`#borrar`);
const botonIgual = document.querySelector(`#botonIgual`);
opSumar.addEventListener(`click`, e => {
if (cajaResultado.textContent !== "") {
e.preventDefault()
} else operacion.textContent = opSumar.value;
})
opRestar.addEventListener(`click`, e => {
if (cajaResultado.textContent !== "") {
e.preventDefault()
} else operacion.textContent = opRestar.value;
})
opMultiplicar.addEventListener(`click`, e => {
if (cajaResultado.textContent !== "") {
e.preventDefault()
} else operacion.textContent = opMultiplicar.value;
})
opDividir.addEventListener(`click`, e => {
if (cajaResultado.textContent !== "") {
e.preventDefault()
} else operacion.textContent = opDividir.value;
})
const uno = document.querySelector(`#uno`);
const dos = document.querySelector(`#dos`);
const tres = document.querySelector(`#tres`);
const cuatro = document.querySelector(`#cuatro`);
const cinco = document.querySelector(`#cinco`);
const seis = document.querySelector(`#seis`);
const ciete = document.querySelector(`#ciete`);
const ocho = document.querySelector(`#ocho`);
const nueve = document.querySelector(`#nueve`);
const cero = document.querySelector(`#cero`);
const addNumbers = n => {
if (operacion.textContent == "") {
primerValor.textContent += n.value;
} else {
segundoValor.textContent += n.value;
}
}
uno.addEventListener(`click`, e => {
if (cajaResultado.textContent !== "") {
e.preventDefault()
} else addNumbers(uno);
})
dos.addEventListener(`click`, e => {
if (cajaResultado.textContent !== "") {
e.preventDefault()
} else addNumbers(dos);
})
tres.addEventListener(`click`, e => {
if (cajaResultado.textContent !== "") {
e.preventDefault()
} else addNumbers(tres);
})
cuatro.addEventListener(`click`, e => {
if (cajaResultado.textContent !== "") {
e.preventDefault()
} else addNumbers(cuatro);
})
cinco.addEventListener(`click`, e => {
if (cajaResultado.textContent !== "") {
e.preventDefault()
} else addNumbers(cinco);
})
seis.addEventListener(`click`, e => {
if (cajaResultado.textContent !== "") {
e.preventDefault()
} else addNumbers(seis);
})
ciete.addEventListener(`click`, e => {
if (cajaResultado.textContent !== "") {
e.preventDefault()
} else addNumbers(ciete);
})
ocho.addEventListener(`click`, e => {
if (cajaResultado.textContent !== "") {
e.preventDefault()
} else addNumbers(ocho);
})
nueve.addEventListener(`click`, e => {
if (cajaResultado.textContent !== "") {
e.preventDefault()
} else addNumbers(nueve);
})
cero.addEventListener(`click`, e => {
if (cajaResultado.textContent !== "") {
e.preventDefault()
} else addNumbers(cero);
})
opBorrar.addEventListener(`click`, e => {
primerValor.textContent = "";
segundoValor.textContent = "";
operacion.textContent = "";
cajaResultado.textContent = "";
})
const realizarOP = e => {
let v1 = primerValor.textContent;
let v2 = segundoValor.textContent;
if (operacion.textContent == "+") {
return calc.sumar(v1, v2)
} else if (operacion.textContent == "-") {
return calc.restar(v1, v2)
} else if (operacion.textContent == "*") {
return calc.multiplicar(v1, v2)
} else if (operacion.textContent == "/") {
return calc.dividir(v1, v2)
} else {
return calc.sumar(v1, v2)
}
}
botonIgual.addEventListener(`click`, e => {
if (primerValor.textContent == "" && segundoValor.textContent == "") {
alert(`ingresa un valor`)
} else {
const res = realizarOP();
cajaResultado.textContent = res;
if (cajaResultado !== "") {
operacion.textContent = "";
primerValor.textContent = "";
segundoValor.textContent = "";
}
}
})