Ver Mensaje Individual
  #1 (permalink)  
Antiguo 14/01/2008, 10:52
_jesic@
 
Fecha de Ingreso: marzo-2007
Mensajes: 19
Antigüedad: 17 años, 2 meses
Puntos: 0
¿Como Reducir Lineas de Codigo?

Hola a todos.
Deseo poner en mi pagina imagenes aleatorias sin que estas se repitan, pues bien, encontre el codigo de abajo en este mismo foro, el problema biene aqui: quiero poner por lo menos 20 imagenes y veo que con 3 ya hay demaciadas comparciones "if" (con 20 imagenes seria un codigo interminable). ¿Hay alguna manera de reducir el codigo, tengo la corazonada que con "for" se podia solucionar.

Saludos.

Gracias.

<html>
<head>
<title>
4 imágenes sin repetir
</title>
<script>
var imagenes = [
"f:/Imágenes/DragonBall/028.jpg",
"f:/Imágenes/DragonBall/054.jpg",
"f:/Imágenes/DragonBall/082.jpg",
"f:/Imágenes/DragonBall/boo_02.jpg",
"f:/Imágenes/DragonBall/boo_03.jpg",
"f:/Imágenes/DragonBall/boo_04.jpg",
"f:/Imágenes/DragonBall/boo_05.jpg"
];

var enlaces = [
"e:/DragonBall/028.jpg",
"http://www.sucaricatura.com/2002/mini/2002H002.jpg",
"http://www.sucaricatura.com/2002/maxi/2002H003.jpg",
"http://www.sucaricatura.com/2002/mini/2002H004.jpg",
"http://www.sucaricatura.com/2002/maxi/2002H005.jpg",
"http://www.sucaricatura.com/2002/mini/2002H006.jpg",
"http://www.sucaricatura.com/2002/maxi/2002H007.jpg"
];

function azar() {
var temp = new Array(4);
temp[0] = Math.floor(Math.random() * imagenes.length);
do
temp[1] = Math.floor(Math.random() * imagenes.length);
while (temp[0] == temp[1])
do
temp[2] = Math.floor(Math.random() * imagenes.length);
while (temp[0] == temp[1] || temp[0] == temp[2] || temp[1] == temp[2])
do
temp[3] = Math.floor(Math.random() * imagenes.length);
while ( temp[0] == temp[1] ||
temp[0] == temp[2] ||
temp[1] == temp[2] ||
temp[0] == temp[3] ||
temp[1] == temp[3] ||
temp[2] == temp[3]
)
// alert(document.links.length);
document.getElementById("enlace1").setAttribute("h ref", enlaces[temp[0]]);
document.getElementById("enlace2").href = enlaces[temp[1]];
document.getElementById("enlace3").href = enlaces[temp[2]];
document.getElementById("enlace4").href = enlaces[temp[3]];
document.images.imagen1.src = imagenes[temp[0]];
document.images.imagen2.src = imagenes[temp[1]];
document.images.imagen3.src = imagenes[temp[2]];
document.images.imagen4.src = imagenes[temp[3]];
}
</script>
</head>
<body onload=azar()>
<a href="#" name="enlace1">
&nbsp;
</a>
<a href="" name=enlace2>
</a>
<a href="" name=enlace3>
</a>
<table border="1" width="100%">
<tr>
<td width="25%">
<a href="#" name="enlace1">
<img src="" name=imagen1>
</a>
</td>
<td width="25%">
<a href="" name=enlace2>
<img src="" name=imagen2>
</a>
</td>
<td width="25%">
<a href="" name=enlace3>
<img src="" name=imagen3>
</a>
</td>
<td width="25%">
<a href="" name=enlace4>
<img src="" name=imagen4>
</a>
</td>
</tr>
</table>
</body>
</html>