-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
404 lines (369 loc) · 14.4 KB
/
index.html
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
<!DOCTYPE html>
<html lang="es">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<!-- Bootstrap CSS only -->
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.2.1/dist/css/bootstrap.min.css" rel="stylesheet"
integrity="sha384-iYQeCzEYFbKjA/T2uDLTpkwGzCiq6soy8tYaI1GyVh/UjpbCx/TYkiZhlZB6+fzT" crossorigin="anonymous">
<!--Link css-->
<link rel="stylesheet" type="text/css" href="./css/style.css">
<!--link js-->
<script src="./js/javascript.js"></script>
<title>Calculadora</title>
</head>
<body>
<h1 class="text-center m-3">Calculadora</h1>
<!-- HTML Calculadora-->
</div>
<div class="container-sm w-50 bg-secondary text-white p-3 border rounded-2 ">
<div class="row bg-white border border-white m-0 p-0">
<div class="col-5 m-0 p-0">
<input class="w-100 border border-white text-center bg-white tex-back" type="text" name="display" placeholder="Ingrese número" id="display1" disabled>
</div>
<div class="col-2 m-0 p-0">
<input class="w-100 border border-white text-center" type="text" name="display" placeholder="Cuentas" id="display2">
</div>
<div class="col-5 m-0 p-0">
<input class="w-100 border border-white text-center" type="text" name="display" placeholder="Notas" id="display3">
</div>
<div class="col-12 m-0 p-0">
<input class="border border border-white w-100 rounded-2 text-center" rows="5" type="text" name="resultado" id="display_resultado" placeholder="Resultado">
</div>
</div>
<div class="row">
<div class="col-6">
<input class="btn w-100 m-1 btn-primary" type="button" value="+" id="sumar">
<input class="btn w-100 m-1 btn-primary" type="button" value="-" id="restar">
</div>
<div class="col-6">
<input class="btn w-100 m-1 btn-primary" type="button" value="*" id="multiplicar">
<input class="btn w-100 m-1 btn-primary" type="button" value="/" id="dividir">
</div>
<div class="row">
<div class="col-4">
<input class="btn w-100 m-1 btn-primary" type="button" value="7" id="tecla_7">
<input class="btn w-100 m-1 btn-primary" type="button" value="4" id="tecla_4">
<input class="btn w-100 m-1 btn-primary" type="button" value="1" id="tecla_1">
</div>
<div class="col-4">
<input class="btn w-100 m-1 btn-primary" type="button" value="8" id="tecla_8">
<input class="btn w-100 m-1 btn-primary" type="button" value="5" id="tecla_5">
<input class="btn w-100 m-1 btn-primary" type="button" value="2" id="tecla_2">
</div>
<div class="col-4">
<input class="btn w-100 m-1 btn-primary" type="button" value="9" id="tecla_9">
<input class="btn w-100 m-1 btn-primary" type="button" value="6" id="tecla_6">
<input class="btn w-100 m-1 btn-primary" type="button" value="3" id="tecla_3">
</div>
<div class="col-4">
<input class="btn w-100 m-1 btn-primary" type="button" value="0" id="tecla_0">
</div>
</div>
<div class="col-12 d-flex justify-content-start"></div>
<input class="btn w-50 m-1 btn-warning" type="button" value="=" id="enter_btn">
<input class="btn w-25 justify-content-end m-1 btn-danger" type="button" value="Borrar" id="borrar">
</div>
<div class="row p-1">
</div>
</div>
<!-- JS Calculadora-->
<script>
//Declaración de variables globales al Script
var valor = 0;
var tecla_calculadora;
var resultado1_js;
var resultado2_js;
var num=0;
var parse_tecla, valor2_js, auxiliar;
var operacion_js; //Determina el tipo de operación: operacion_js.value= 1 suma , 2 resta, 3 multiplicación, 4 división
var display1_js = document.getElementById("display1");
var display2_js = document.getElementById("display2");
var display3_js = document.getElementById("display3");
var resultado_display_js = document.getElementById("display_resultado");
var sumar_js = document.getElementById("sumar");
var restar_js = document.getElementById("restar");
var multiplicar_js = document.getElementById("multiplicar");
var dividir_js = document.getElementById("dividir");
var enter_js = document.getElementById("enter_btn");
var borrar_js = document.getElementById("borrar")
var tecla_1_js = document.getElementById("tecla_1")
var tecla_2_js = document.getElementById("tecla_2")
var tecla_3_js = document.getElementById("tecla_3")
var tecla_4_js = document.getElementById("tecla_4")
var tecla_5_js = document.getElementById("tecla_5")
var tecla_6_js = document.getElementById("tecla_6")
var tecla_7_js = document.getElementById("tecla_7")
var tecla_8_js = document.getElementById("tecla_8")
var tecla_9_js = document.getElementById("tecla_9")
var tecla_0_js = document.getElementById("tecla_0")
// ----------------------------------START entrada de teclado----------------------------------
// capturador de teclado
window.onkeydown = teclado_calculadora; // Esta linea hace que cada vez que se presione una tecla (cualquiera) llame a la función "teclado_calculadora"
function teclado_calculadora() { //función que realiza acciones dependiendo de la tecla presionada
/* Keyboard -> tecla_calculadora : el nombre de la tecla presionada por teclado se guarda ( y se sobreescribe) en esta variable
llamada "tecla_calculadora". Ej si se presiona intro-> tecla_calculadora=Enter, si se presiona la tecla Esc-> tecla_calculadora=Escape, etc.*/
tecla_calculadora = event.key;
/*Se parsea la entrada de teclado, parse_tecla siempre es de tipo numérico.
Obs: Si la entrada de teclado es no numérica(ejemplo Enter, + , m, etc), parse_tecla, al estar parseado toma el valor "NaN" (not a number)*/
parse_tecla = parseFloat(tecla_calculadora); // parse_tecla= parseo de la tecla ingresada (tecla_calculadora)
console.log("tecla= "+ tecla_calculadora);
console.log("type tecla_calculadora " + typeof tecla_calculadora)
console.log("parseFloat tecla_calculadora: " + parseFloat(tecla_calculadora))
parse_tecla = parseInt(tecla_calculadora)
console.log("parse_tecla : " + parse_tecla)
console.log("type parse_tecla " + typeof parse_tecla)
if ( parse_tecla >= 0 ) { //"parse_tecla >= 0" para que en el display1 solo entren números y no aparezca un "NaN" cuando se presione una tecla no numérica.
display1_js.value += parse_tecla;
} else {
switch (tecla_calculadora){ //llamado de funciones según tecla presionada
case tecla_calculadora = "+": //si la tecla presionada es "+" llama a la función suma()
suma()
break;
case tecla_calculadora = "-": //si la tecla presionada es "-" llama a la función resta(), etc
resta()
break;
case tecla_calculadora = "*":
multiplicacion()
break;
case tecla_calculadora = "/":
division();
break;
case tecla_calculadora = "Escape": //Si se presiona la tecla Esc llama a la función borrar().
borrar();
break
case tecla_calculadora = "Enter": //presionar Enter o Intro es igual a presionar el botón de "="
operacion(num);
num =0;
break
}
}
}
// ----------------------------------Fin entrada de teclado----------------------------------
// ---------------------------------- Inicio Funciones aritmeticas----------------------------------
//función SUMA
function suma() {
valor1_js = parseFloat(display1_js.value);
if (valor1_js >= 0){
operacion_js = 1;
display1_js.value = "";
display2_js.value = valor1_js + " + ";
display3_js.value = "+";
console.log("operacion_js.value= " + operacion_js + " Valor= " + valor1_js + " + ");
} else {
operacion_js = 1;
valor1_js = 0;
display1_js.value = "";
display2_js.value = " 0 + ";
display3_js.value = "+";
console.log("operacion_js.value= " + operacion_js + " Valor= " + valor1_js + " + ");
}
}
//función RESTA
function resta() {
valor1_js = parseFloat(display1_js.value);
operacion_js = 2;
display1_js.value = "";
display2_js.value = valor1_js + " - ";
display3_js.value = "-";
console.log("operacion_js.value= " + operacion_js + " Valor= " + valor1_js + " - ")
}
//función MULTIPLICACION
function multiplicacion() {
valor1_js = parseFloat(display1_js.value)
operacion_js = 3;
display1_js.value = "";
display2_js.value = valor1_js + " * "
display3_js.value = "*";
console.log("operacion_js.value= " + operacion_js + " Valor= " + valor1_js + " * ")
}
//función DIVISION
function division() {
valor1_js = parseFloat(display1_js.value);
operacion_js = 4;
display1_js.value = "";
display2_js.value = valor1_js + " / "
display3_js.value = "/";
console.log("operacion_js.value= " + operacion_js + " Valor= " + valor1_js + " / ")
}
//función BORRAR DISPLAYS
function borrar() {
display1_js.value = 0;
display2_js.value = 0;
display3_js.value = 0;
resultado_display_js.value= 0;
valor1_js = 0;
valor2_js = 0;
operacion_js = 0;
num = 1;
}
// ----------------------------------FIN Funciones aritmeticas----------------------------------
// ----------------------------------Inicio Función de operacións----------------------------------
//Determina el tipo de operación: x = 1 suma , 2 resta, 3 multiplicación, 4 división.
function operacion(num){
if (num = 1){
valor2_js = display1_js.value;
switch (operacion_js){
case operacion_js.value=1: //caso suma
resultado_display_js.value = parseInt(valor1_js) + parseInt(valor2_js);
display1_js.value = resultado_display_js.value;
display2_js.value += valor2_js + " = " + resultado_display_js.value
console.log("Operación 2 Valor 1= "+ valor1_js + " Valor 2= " + valor2_js + ". El resultado de la suma es: " + resultado_display_js.value)
break;
case operacion_js.value=2: //caso resta
resultado_display_js.value = parseInt(valor1_js) - parseInt(valor2_js);
display1_js.value = resultado_display_js.value;
display2_js.value += valor2_js + " = " + resultado_display_js.value
display3_js.value = "-";
console.log("Operación 2 Valor 1= "+ valor1_js + " Valor 2= " + valor2_js + ". El resultado de la resta es: " + resultado_display_js.value)
break;
case operacion_js.value=3: //caso multiplicacion
resultado_display_js.value = parseInt(valor1_js) * parseInt(valor2_js);
display1_js.value = resultado_display_js.value;
display2_js.value += valor2_js + " = " + resultado_display_js.value
display3_js.value = "*";
console.log("Operación 2 Valor 1= "+ valor1_js + " Valor 2= " + valor2_js + ". El resultado de la multiplicacion es: " + resultado_display_js.value)
break;
case operacion_js.value=4: //caso división
resultado_display_js.value = parseInt(valor1_js) / parseInt(valor2_js);
display1_js.value = resultado_display_js.value;
display2_js.value += valor2_js + " = " + resultado_display_js.value
display3_js.value = "/";
console.log("Operación 2 Valor 1= "+ valor1_js + " Valor 2= " + valor2_js + ". El resultado de la division es: " + resultado_display_js.value)
break;
}
}
}
// ----------------------------------FIN funcion operación----------------------------------
// ----------------------------------Inicio función botton_num que escribe el botón numero seleccionado:----------------------------------
function button_num(x){
switch(x){
case x = 1:
auxiliar = 1;
display1_js.value += parseFloat(auxiliar);
console.log("x= " + x)
break;
case x = 2:
auxiliar = 2;
display1_js.value += parseFloat(auxiliar);
console.log("x= " + x)
break;
case x = 3:
auxiliar = 3;
display1_js.value += parseFloat(auxiliar);
console.log("x= " + x)
break;
case x = 4:
auxiliar = 4;
display1_js.value += parseFloat(auxiliar);
console.log("x= " + x)
break;
case x = 5:
auxiliar = 5;
display1_js.value += parseFloat(auxiliar);
console.log("x= " + x)
break;
case x = 6:
auxiliar = 6;
display1_js.value += parseFloat(auxiliar);
console.log("x= " + x)
break;
case x = 7:
auxiliar = 7;
display1_js.value += parseFloat(auxiliar);
console.log("x= " + x)
break;
case x = 8:
auxiliar = 8;
display1_js.value += parseFloat(auxiliar);
console.log("x= " + x)
break;
case x = 9:
auxiliar = 9;
display1_js.value += parseFloat(auxiliar);
console.log("x= " + x)
break;
case x = 0:
display1_js.value += parseFloat(auxiliar);
console.log("x= " + x)
break;
}
}
// ----------------------------------FIN funcion botton_num----------------------------------
/*----------------------Inicio EventListener ----> Click's Botones Números----------------------*/
//botón 1
tecla_1_js.addEventListener("click", function(){
button_num(1)
})
//botón 2
tecla_2_js.addEventListener("click", function(){
button_num(2)
})
//botón 2
tecla_3_js.addEventListener("click", function(){
button_num(3)
})
//botón 4
tecla_4_js.addEventListener("click", function(){
button_num(4)
})
//botón 5
tecla_5_js.addEventListener("click", function(){
button_num(5)
})
//botón 6
tecla_6_js.addEventListener("click", function(){
button_num(6)
})
//botón 7
tecla_7_js.addEventListener("click", function(){
button_num(7)
})
//botón 8
tecla_8_js.addEventListener("click", function(){
button_num(8)
})
//botón 9
tecla_9_js.addEventListener("click", function(){
button_num(9)
})
//botón 0
tecla_0_js.addEventListener("click", function(){
button_num(0)
})
/*----------------------FIN EventListener ----> Click's Botones Números----------------------*/
/*----------------------inicio Click botones de operaciones----------------------*/
/* Click botón "+"" */
sumar_js.addEventListener("click", function(){
suma()
})
/* Click botón "-"" */
restar_js.addEventListener("click", function(){
resta()
})
/* Click botón "*"" */
multiplicar_js.addEventListener("click", function(){
multiplicacion()
})
/* Click botón "/" */
dividir_js.addEventListener("click", function(){
division()
})
/* Click botón "Borrar" */
borrar_js.addEventListener("click", function(){
borrar()
})
/* Click botón "=" */
enter_js.addEventListener("click", function(){
/* */
operacion(1) //la función siempre tiene el parámetro=1 para que se ejecute siempre que se haga click en el botón "="
})
/*----------------------FIN Click botones de operaciones----------------------*/
</script>
<!-- JavaScript Bundle with Popper Bootstrap-->
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.2.1/dist/js/bootstrap.bundle.min.js"
integrity="sha384-u1OknCvxWvY5kfmNBILK2hRnQC3Pr17a+RTT6rIHI7NnikvbZlHgTPOOmMi466C8"
crossorigin="anonymous"></script>
</body>
</html>