JavaScript - calculadora

 
Vista:
sin imagen de perfil

calculadora

Publicado por Julio Cesar (16 intervenciones) el 14/07/2017 21:14:54
Solo quisiera que evaluaran esta mini-calculadora que hecho para practicar un poco el lenguaje. Busquen algun error o denme algun consejo. Tambien no sé como hacer que el punto sea tratado como parte de un numero.

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
<!DOCTYPE html>
<html>
<head>
	<title>CALCULADORA</title>
	<style>
    body{
    	font-family: sans-serif;
    	background-color: #123;
    }
    #calculadora{
    	background-color: #125;
    	width: 250px;
    	height: 350px;
    	border: 4px solid #fff;
    	display: flex;
    	justify-content: center;
    	align-content: flex-start;
    	flex-wrap: wrap;
    	margin: auto;
    }
 
    #display{
    	width: 230px;
    	height: 40px;
    	background-color: #000;
    	color: #fff;
    	border: 1px solid #fff;
    	font-size: 2em;
    	margin-top: 3px;
    	margin-bottom: 3px;
    	text-align: right;
    }
 
    .numero{
    	width: 55px;
    	height: 55px;
    	margin: 2px;
    }
 
    .operadores{
      	width: 55px;
    	height: 55px;
    	margin: 2px;
    }
 
    .nose, .rst{
      	width: 55px;
    	height: 55px;
    	margin: 2px;
    }
 
    .igual{
    	width: 114px;
    	height: 55px;
    	margin: 2px;
    	font-size: 3em;
    }
 
    input{
    	font-size: 2em;
    }
 
	</style>
	<script src="calculadora.js"></script>
 
</head>
<body>
 
<div id="calculadora">
<input type="text" id="display">
<input type="button" class="operadores" value="+" onclick="suma();">
<input type="button" class="operadores" value="-" onclick="resta();">
<input type="button" class="operadores" value="x" onclick="multiplicacion();">
<input type="button" class="operadores" value="÷" onclick="division();">
<input type="button" class="numero" value="9" onclick="valordisplay(9)">
<input type="button" class="numero" value="8" onclick="valordisplay(8)">
<input type="button" class="numero" value="7" onclick="valordisplay(7)">
<input type="button" class="rst" value="R" onclick="resetear();">
<input type="button" class="numero" value="6" onclick="valordisplay(6)">
<input type="button" class="numero" value="5" onclick="valordisplay(5)">
<input type="button" class="numero" value="4" onclick="valordisplay(4)">
<input type="button" class="nose">
<input type="button" class="numero" value="3" onclick="valordisplay(3)">
<input type="button" class="numero" value="2" onclick="valordisplay(2)">
<input type="button" class="numero" value="1" onclick="valordisplay(1)">
<input type="button" class="nose">
<input type="button" class="numero" value="0" onclick="valordisplay(0)">
<input type="button" class="numero" value="." onclick="valordisplay(.)">
<input type="button" class="igual" value="=" onclick="resultado()">
</div>
<script>
document.getElementById("display").value=0;
</script>
</body>
</html>

JAVASCRIPT:

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
var numero="";
var acumulado=0;
var primera_operacion=true;
var sumar=false;
var restar=false;
var multi=false;
var divi=false;
 
 
function valordisplay(digito) {
    document.getElementById('display').value=numero+digito;
    numero=document.getElementById('display').value;
}
 
 
function multiplicacion() {
    if (primera_operacion==false){
        if (sumar) {
            document.getElementById("display").value=acumulado;
        }else if (restar) {
            acumulado=acumulado-parseInt(numero);
            document.getElementById("display").value=acumulado;
        }else if (divi) {
            acumulado=acumulado/parseInt(numero);
            document.getElementById("display").value=acumulado;
        }else{
            acumulado=acumulado*parseInt(numero);
            document.getElementById("display").value=acumulado;
        }
    }else{
        acumulado=parseInt(numero);
        primera_operacion=false;
    }
        multi=true;
        sumar=false; restar=false;;
        numero="";
}
 
function suma() {
    if (restar) {
        acumulado=acumulado-parseInt(numero);
        document.getElementById("display").value=acumulado;
    }else if (multi) {
        acumulado=acumulado*parseInt(numero);
        document.getElementById("display").value=acumulado;
    }else if (divi) {
        acumulado=acumulado/parseInt(numero);
        document.getElementById("display").value=acumulado;
    }else{
        acumulado=acumulado+parseInt(numero);
        document.getElementById("display").value=acumulado;
    }
    numero="";
    primera_operacion=false;
    sumar=true;
    restar=false; multi=false;
}
 
function resta() {
    if(primera_operacion==false){
        if (sumar) {
        	acumulado=acumulado+parseInt(numero);
        	document.getElementById("display").value=acumulado;
        }else if (multi) {
            acumulado=acumulado*parseInt(numero);
            document.getElementById("display").value=acumulado;
        }else if (divi) {
            acumulado=acumulado/parseInt(numero);
            document.getElementById("display").value=acumulado;
        }else{
        	acumulado=acumulado-parseInt(numero);
            document.getElementById("display").value=acumulado;
        }
    }else{
        acumulado=parseInt(numero);
        primera_operacion=false;
    }
    numero="";
    sumar=false;
    restar=true; mutli=false;
}
 
function division(){
    if (primera_operacion==false){
        if (sumar) {
           acumulado=acumulado+parseInt(numero);
           document.getElementById('display').value=acumulado;
        }else if(restar) {
           acumulado=acumulado-parseInt(numero);
           document.getElementById('display').value=acumulado;
        }else if (multi) {
            acumulado=acumulado*parseInt(numero);
            document.getElementById('display').value=acumulado;
        }else{
            acumulado=acumulado/parseInt(numero);
        }
    }else{
        acumulado=parseInt(numero);
        primera_operacion=false;
    }
    numero="";
    divi=true;
    sumar=false;restar=false;multi=false;
}
 
function multiplicacion() {
    if (primera_operacion==false){
        if (sumar) {
            document.getElementById("display").value=acumulado;
        }else if (restar) {
            acumulado=acumulado-parseInt(numero);
            document.getElementById("display").value=acumulado;
        }else if (divi) {
            acumulado=acumulado/parseInt(numero);
            document.getElementById("display").value=acumulado;
        }else{
            acumulado=acumulado*parseInt(numero);
            document.getElementById("display").value=acumulado;
        }
    }else{
        acumulado=parseInt(numero);
        primera_operacion=false;
    }
        multi=true;
        sumar=false; restar=false;;
        numero="";
}
 
function resultado(){
	if(sumar){
		document.getElementById("display").value=acumulado+parseInt(numero);
        numero=0;
	}else if(restar){
 		document.getElementById("display").value=acumulado-parseInt(numero);
        numero=0;
    }else if(multi){
        document.getElementById("display").value=acumulado*parseInt(numero);
        numero=1;
    }else if(divi){
        document.getElementById("display").value=acumulado/parseInt(numero);
        numero=1;
    }
    acumulado=parseInt(document.getElementById('display').value);
}
 
function resetear(){
    numero="";
    acumulado=0;
    primera_operacion=true;
    sumar=false;
    restar=false;
    multi=false;
	document.getElementById("display").value="";
}
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder