JavaScript - No encuentro el error en este programa javascript-

 
Vista:

No encuentro el error en este programa javascript-

Publicado por Txus (4 intervenciones) el 14/10/2013 18:19:19
Hola,

Necesito ayuda con este problema. Esto es una máquina tragaperras en html con Javascript. Algo básico y muy simple. Pero tengo un error en la función premios() y es que no sucede nada en la primera comparación de cada if. No sé por qué es y he probado mil cosas. A ver si alguien puede ayudarme porque no encuentro nada parecido en ningún sitio. Debe ser un error muy tonto que se me ha pasado por alto. Llevo muy poco tiempo con la programación.

Aquí está el código completo, la función está al final del script.Muchas gracias y un saludo.
-----------------------------------------------------------------------------------------------------------------------------------
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
<html>
 
	<head><title>Maquina tragaperras que no traga-perras</title></head>
 
	<body style="width:1240px; height:800;" >
<!-- ******************************************************************** -->
<!-- ******************************************************************** -->
<SCRIPT LANGUAGE=JavaScript>
 
//Variables globales del script:
var decimas=0;
var nAleatorio=0;
var Imagenes= new Array();
var noRepetir1=-1;
var noRepetir2=-1;
var noRepetir3=-1;
 
//Aquí asignamos al array las imágenes de la máquina tragaperras:
Imagenes [0]= "1.jpg";
Imagenes [1]= "2.jpg";
Imagenes [2]= "3.jpg";
Imagenes [3]= "4.jpg";
Imagenes [4]= "5.jpg";
Imagenes [5]= "6.jpg";
Imagenes [6]= "7.jpg";
Imagenes [7]= "8.jpg";
 
//Función que activa el juego al pulsar el botón:
function inicio()
{
decimas=0;
nAleatorio=0;
noRepetir1=-1;
noRepetir2=-1;
noRepetir3=-1;
 
document.getElementById('boton').disabled=true;
restoCredito();
visualizo();
}
 
//Función que ejecuta la máquina tragaperras tras pulsar el botón
function visualizo()
{
	decimas+=1;
//Con estos if controlo el movimiento-parada de las figuras y que no se repitan para que no haya efectos raros:		 
	if (decimas<20)
		{
			nAleatorio=Math.floor(Math.random() * (8 - 1)+1) + 1;
 
			if(nAleatorio==noRepetir1){
 
				nAleatorio-=1;
 
			if( nAleatorio<0){
				nAleatorio+=1;}
 
			document.getElementById('IMAGEN1').src=Imagenes[nAleatorio-1];
			}
			else {
				document.getElementById('IMAGEN1').src=Imagenes[nAleatorio-1];
			}
			noRepetir1=nAleatorio;
	    }
 
	if (decimas<30)
		{
		    nAleatorio=Math.floor(Math.random() * (8 - 1)+1) + 1;
 
			if(nAleatorio==noRepetir2){
 
				nAleatorio-=1;
 
			if( nAleatorio<0){
				nAleatorio+=1;}
			document.getElementById('IMAGEN2').src=Imagenes[nAleatorio-1];
			}
			else {
				document.getElementById('IMAGEN2').src=Imagenes[nAleatorio-1];
			}
			noRepetir2=nAleatorio;
	    }
 
	if (decimas<40)
		{
		   nAleatorio=Math.floor(Math.random() * (8 - 1)+1) + 1;
 
			if(nAleatorio==noRepetir3){
 
				nAleatorio-=1;
 
			if( nAleatorio<0){
				nAleatorio+=1;}
			document.getElementById('IMAGEN3').src=Imagenes[nAleatorio-1];
			}
			else {
				document.getElementById('IMAGEN3').src=Imagenes[nAleatorio-1];
			}
			noRepetir3=nAleatorio;
 
			setTimeout("visualizo()", 100);
	    }
//Aquí controlaré cuando se activa el botón y cuándo sumo los premios:		
	else
		{
		    if(document.getElementById('CREDITO').value==0)
				document.getElementById('boton').disabled=true;
 
				else
					document.getElementById('boton').disabled=false;
		premio();
		}
}
 
//Aquí controlamos que nos reste crédito cada vez que pulsamos el botón:
function restoCredito()
{
document.getElementById('CREDITO').value-=10;
}
 
//Aquí controlamos las operaciones referentes a los premios:
function premio()
{
//Variables que utilizo para ahorrarme escribir tantas veces el mismo comando:
	var imagen1 = document.getElementById('IMAGEN1').src;
	var imagen2 = document.getElementById('IMAGEN2').src;
	var imagen3 = document.getElementById('IMAGEN3').src;
 
/*
//Esta parte la he utilizado para comprobar el funcionamiento de los premios:
	if(imagen1!=Imagenes[1] && imagen1!=imagen2 && imagen2!=imagen3){
		document.getElementById('CREDITO').value=parseInt(document.getElementById('CREDITO').value)+100;
		alert("PREMIO");
		}
*/
 
//Estos son los premios cuando salen 3 figuras iguales:	
	if(imagen1==Imagenes[1]){// && imagen1==imagen2 && imagen2==imagen3){
		document.getElementById('CREDITO').value=parseInt(document.getElementById('CREDITO').value)+20;
		alert("PREMIO");
	}
 
	if(imagen1==Imagenes[4] && imagen1==imagen2 && imagen2==imagen3){
		document.getElementById('CREDITO').value=parseInt(document.getElementById('CREDITO').value)+40;
		alert("PREMIO");
	}
 
	if(imagen1==Imagenes[5] && imagen1==imagen2 && imagen2==imagen3){
		document.getElementById('CREDITO').value=parseInt(document.getElementById('CREDITO').value)+60;
		alert("PREMIO");
	}
 
	if(imagen1==Imagenes[3] && imagen1==imagen2 && imagen2==imagen3){
		document.getElementById('CREDITO').value=parseInt(document.getElementById('CREDITO').value)+80;
		alert("PREMIO");
	}
 
	if(imagen1==Imagenes[7] && imagen1==imagen2 && imagen2==imagen3){
		document.getElementById('CREDITO').value=parseInt(document.getElementById('CREDITO').value)+5000;
		alert("PREMIO GORDO!");
	}
 
//Estos son los premios combinados con la imagen del 7:		
	if(imagen1==Imagenes[1] && imagen1==imagen2 && imagen3==Imagenes[6]){
		document.getElementById('CREDITO').value=parseInt(document.getElementById('CREDITO').value)+100;
		alert("PREMIO!");
	}
 
	if(imagen1==Imagenes[4] && imagen1==imagen2 && imagen3==Imagenes[6]){
		document.getElementById('CREDITO').value=parseInt(document.getElementById('CREDITO').value)+200;
		alert("PREMIO!");
	}
 
	if(imagen1==Imagenes[5] && imagen1==imagen2 && imagen3==Imagenes[6]){
		document.getElementById('CREDITO').value=parseInt(document.getElementById('CREDITO').value)+300;
		alert("PREMIO!");
	}
 
	if(imagen1==Imagenes[3] && imagen1==imagen2 && imagen3==Imagenes[6]){
		document.getElementById('CREDITO').value=parseInt(document.getElementById('CREDITO').value)+400;
		alert("PREMIO");
	}
}
 
</SCRIPT>
<!-- ******************************************************************** -->
<!-- ******************************************************************** -->
 
 
			<div style = "border:black 0px solid; display:table-cell; width:1240px; position:absolute; top: 50%; left: 50%; margin-top:-400px; margin-left:-620px;">
 
				<input type="text" id="CREDITO" value="100" style="border:0px; position:relative; top:373; left:252; width:140px; height:40px;
				font-family:Goudy Stout; font-size:1.3em; text-shadow: 1px 1px 1px rgba(0,0,0,0.65); text-align:center;" readonly="disabled"/>
				
				<div style = "border:blue 0px solid  ; float:left ; height:800px; width:300px;" >
					<center>
						<img id="PREMIOS1" name="PREMIOS1" src="premios.jpg" border=0 style="width:100%; height:100%;" >
					</center>
				</div>
				<div style = "border:blue 0px solid  ; float:right; height:800px; width:300px;">
					<center>
						<img id="PREMIOS2" name="PREMIOS2" src="premios.jpg" border=0 style="width:100%; height:100%;" >
					</center>
				</div>
				<div style = "border:red 0px solid   ; float:left ; height:245px; width:620px; margin-left:13px;" >
					<center>
						<img id="LOGO" name="LOGO" src="LogoJackPot.jpg" border=0 style="width:100%; height:100%;" >
					</center>
				</div>
				<div style = "border:pink 0px solid  ; float:left ; height:150px; width:620  ; padding:4px; margin-left:8px; margin-top:30px" >
					<center>
						<img id="CREDITO" name="CREDITO" src="credito.jpg" border=0 style="padding:40px" >
					</center>
				</div>
				<div style = "border:red 0px solid   ; float:left ; height:340px; width:620px;  margin-left:13px; margin-top:30px" >
					<center>
						<img id="IMAGEN1" name="IMAGEN1" src="7.jpg" border=1 >
						<img id="IMAGEN2" name="IMAGEN2" src="7.jpg" border=1 >
						<img id="IMAGEN3" name="IMAGEN3" src="7.jpg" border=1 >
						<br><br>
						<input type="button" id="boton" name="boton" value="  Juegue y Suerte  "
						style="width:300px; height:60px;" onclick="inicio()"/>
					</center>
				</div>
				
				
			</div>
			
		
		
	<body>
	
</html>
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
Imágen de perfil de xve
Val: 3.162
Oro
Ha mantenido su posición en JavaScript (en relación al último mes)
Gráfica de JavaScript

No encuentro el error en este programa javascript-

Publicado por xve (2100 intervenciones) el 14/10/2013 18:26:32
Hola Txus, no hay ninguna función denominada premios()...
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar

No encuentro el error en este programa javascript-

Publicado por Txus (4 intervenciones) el 14/10/2013 18:30:34
premio(), perdón quería decir premio()

el programa entra, si hago la prueba poniento todas las comparaciones en != funciona y saltan los premios, cuando pongo todas las comparaciones en == menos la primera (que me compara el valor de la imagen html con el array del script), también funciona. El problema es cuando pongo esa primera comparación en == para que me devuelva true. Ahí ya no saltan los premios.

Gracias
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar

No encuentro el error en este programa javascript-

Publicado por Txus (4 intervenciones) el 14/10/2013 19:39:11
Un dato que no había dado es que el nombre de la imagen en el html, son los mismos que en el array.

Muchas gracias y un saludo.
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar

No encuentro el error en este programa javascript-

Publicado por Txus (4 intervenciones) el 15/10/2013 15:42:55
Después de darle vueltas he cambiado totalmente la forma de afrontar el tema, he hecho esto, que en realidad es mucho mejor, y me funciona de maravilla. Aunque si alguien me puede deicr el por qué de la otra forma no funciona, lo agradecería. Un saludo.

Si alquien quiere probarlo, sólo tiene que meter 8 imágenes llamadas 1.jpg, 2.jpg, 3.jpg, etc. en los divs unas imágenes para que quede bonito jeje. Gracias

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
<html>
 
	<head><title>Maquina tragaperras que no traga-perras</title></head>
 
	<body style="width:1240px; height:800;" >
<!-- ******************************************************************** -->
<!-- ******************************************************************** -->
<SCRIPT LANGUAGE=JavaScript>
 
//Variables globales del script:
var decimas=0;
var nAleatorio=0;
var Imagenes= new Array();
var noRepetir1=-1;
var noRepetir2=-1;
var noRepetir3=-1;
var imagen1=0;
var imagen2=0;
var imagen3=0;
 
//Aquí asignamos al array las imágenes de la máquina tragaperras:
Imagenes [0]= "1.jpg";
Imagenes [1]= "2.jpg";
Imagenes [2]= "3.jpg";
Imagenes [3]= "4.jpg";
Imagenes [4]= "5.jpg";
Imagenes [5]= "6.jpg";
Imagenes [6]= "7.jpg";
Imagenes [7]= "8.jpg";
 
//Función que activa el juego al pulsar el botón:
function inicio()
{
decimas=0;
nAleatorio=0;
noRepetir1=-1;
noRepetir2=-1;
noRepetir3=-1;
imagen1=0;
imagen2=0;
imagen3=0;
 
document.getElementById('boton').disabled=true;
restoCredito();
visualizo();
}
 
//Función que ejecuta la máquina tragaperras tras pulsar el botón
function visualizo()
{
	decimas+=1;
//Con estos if controlo el movimiento-parada de las figuras y que no se repitan para que no haya efectos raros:		 
	if (decimas<20)
		{
			nAleatorio=Math.floor(Math.random() * (8 - 1)) + 1;
 
			if(nAleatorio==noRepetir1){
 
				nAleatorio-=1;
 
			if( nAleatorio<0){
				nAleatorio+=1;}
 
			document.getElementById('IMAGEN1').src=Imagenes[nAleatorio];
			imagen1=nAleatorio;
			}
			else {
				document.getElementById('IMAGEN1').src=Imagenes[nAleatorio];
				imagen1=nAleatorio;
			}
			noRepetir1=nAleatorio;
	    }
 
	if (decimas<30)
		{
		    nAleatorio=Math.floor(Math.random() * (8 - 1)) + 1;
 
			if(nAleatorio==noRepetir2){
 
				nAleatorio-=1;
 
			if( nAleatorio<0){
				nAleatorio+=1;}
			document.getElementById('IMAGEN2').src=Imagenes[nAleatorio];
			imagen2=nAleatorio;
			}
			else {
				document.getElementById('IMAGEN2').src=Imagenes[nAleatorio];
				imagen2=nAleatorio;
			}
			noRepetir2=nAleatorio;
	    }
 
	if (decimas<40)
		{
		   nAleatorio=Math.floor(Math.random() * (8 - 1)) + 1;
 
			if(nAleatorio==noRepetir3){
 
				nAleatorio-=1;
 
			if( nAleatorio<0){
				nAleatorio+=1;}
			document.getElementById('IMAGEN3').src=Imagenes[nAleatorio];
			imagen3=nAleatorio;
			}
			else {
				document.getElementById('IMAGEN3').src=Imagenes[nAleatorio];
				imagen3=nAleatorio;
			}
			noRepetir3=nAleatorio;
 
			setTimeout("visualizo()", 100);
	    }
//Aquí controlaré cuando se activa el botón y cuándo sumo los premios:		
	else
		{
		    if(document.getElementById('CREDITO').value==0)
				document.getElementById('boton').disabled=true;
 
				else
					document.getElementById('boton').disabled=false;
 
			premio(imagen1,imagen2,imagen3);
		}
}
 
//Aquí controlamos que nos reste crédito cada vez que pulsamos el botón:
function restoCredito()
{
document.getElementById('CREDITO').value-=10;
}
 
//Aquí controlamos las operaciones referentes a los premios:
function premio(img1,img2,img3)
{
//Estos son los premios cuando salen 3 figuras iguales:	
	if((img1==1) && (img1==img2) && (img2==img3)){
		document.getElementById('CREDITO').value=parseInt(document.getElementById('CREDITO').value)+20;
		alert("PREMIO");
	}
 
	if((img1==4) && (img1==img2) && (img2==img3)){
		document.getElementById('CREDITO').value=parseInt(document.getElementById('CREDITO').value)+40;
		alert("PREMIO");
	}
 
	if((img1==5) && (img1==img2) && (img2==img3)){
		document.getElementById('CREDITO').value=parseInt(document.getElementById('CREDITO').value)+60;
		alert("PREMIO");
	}
 
	if((img1==3) && (img1==img2) && (img2==img3)){
		document.getElementById('CREDITO').value=parseInt(document.getElementById('CREDITO').value)+80;
		alert("PREMIO");
	}
 
	if((img1==7) && (img1==img2) && (img2==img3)){
		document.getElementById('CREDITO').value=parseInt(document.getElementById('CREDITO').value)+5000;
		alert("PREMIO GORDO!");
	}
 
//Estos son los premios combinados con la imagen del 7:		
	if((img1==1) && (img1==img2) && (img3==6)){
		document.getElementById('CREDITO').value=parseInt(document.getElementById('CREDITO').value)+100;
		alert("PREMIO!");
	}
 
	if((img1==4) && (img1==img2) && (img3==6)){
		document.getElementById('CREDITO').value=parseInt(document.getElementById('CREDITO').value)+200;
		alert("PREMIO!");
	}
 
	if((img1==5) && (img1==img2) && (img3==6)){
		document.getElementById('CREDITO').value=parseInt(document.getElementById('CREDITO').value)+300;
		alert("PREMIO!");
	}
 
	if((img1==2) && (img1==img2) && (img3==6)){
		document.getElementById('CREDITO').value=parseInt(document.getElementById('CREDITO').value)+400;
		alert("PREMIO");
	}
}
 
</SCRIPT>
<!-- ******************************************************************** -->
<!-- ******************************************************************** -->
 
 
			<div style = "border:black 0px solid; display:table-cell; width:1240px; position:absolute; top: 50%; left: 50%; margin-top:-400px; margin-left:-620px;">
 
				<input type="text" id="CREDITO" value="100" style="border:0px; position:relative; top:373; left:252; width:140px; height:40px;
				font-family:Goudy Stout; font-size:1.3em; text-shadow: 1px 1px 1px rgba(0,0,0,0.65); text-align:center;" readonly="disabled"/>
				
				<div style = "border:blue 0px solid  ; float:left ; height:710px; width:300px;" >
					<center>
						<img id="PREMIOS1" name="PREMIOS1" src="premios.jpg" border=0 style="width:100%; height:100%;" >
					</center>
				</div>
				<div style = "border:blue 0px solid  ; float:right; height:710px; width:300px;">
					<center>
						<img id="PREMIOS2" name="PREMIOS2" src="premios.jpg" border=0 style="width:100%; height:100%;" >
					</center>
				</div>
				<div style = "border:red 0px solid   ; float:left ; height:245px; width:620px; margin-left:13px;" >
					<center>
						<img id="LOGO" name="LOGO" src="LogoJackPot.jpg" border=0 style="width:100%; height:100%;" >
					</center>
				</div>
				<div style = "border:pink 0px solid  ; float:left ; height:150px; width:620  ; padding:4px; margin-left:8px; margin-top:30px" >
					<center>
						<img id="CREDITO" name="CREDITO" src="credito.jpg" border=0 style="padding:40px" >
					</center>
				</div>
				<div style = "border:red 0px solid   ; float:left ; height:340px; width:620px;  margin-left:13px; margin-top:30px" >
					<center>
						<img id="IMAGEN1" name="IMAGEN1" src="7.jpg" border=1 >
						<img id="IMAGEN2" name="IMAGEN2" src="7.jpg" border=1 >
						<img id="IMAGEN3" name="IMAGEN3" src="7.jpg" border=1 >
						<br><br>
						<input type="button" id="boton" name="boton" value="  Juegue y Suerte  "
						style="width:300px; height:60px; cursor:pointer;" onclick="inicio()"/>
					</center>
				</div>
				
				
			</div>
			
		
		
	<body>
	
</html>
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
1
Comentar