Ver Mensaje Individual
  #8 (permalink)  
Antiguo 04/04/2015, 10:44
Fattony
 
Fecha de Ingreso: abril-2015
Mensajes: 6
Antigüedad: 9 años, 1 mes
Puntos: 0
Respuesta: Atasco con codigo de sorteo

Hola a todos y muchas gracias por vuestras respuestas
Al final he conseguido que funcionara pero no se si el codigo es el mas eficaz que se podria usar. Con esto lo que hago es capturar las tiradas del sorteo con los nombres ya hechas por otro lado y las introduzco en las cajas de texto y al darle al boton Aceptar me ordena las puntuaciones que yo le he metido de mayor a menor y a continuacion añade el nombre de su tirador al lado en mayus.
Os paso el codigo ahroa completo y me decis si os gusta o no. Tened en cuenta que llevo viendo videos 1 mes y no habia hecho esto antes, no seais muy duros jeje
Un saludo y gracias
Código Javascript:
Ver original
  1. var a, a1, b, b2, c, c3, d, d4, e, e5 , f, f6, g, g7, h, h8, i, i9, j, j10, k, k11, l, l12, m, m13, n, n14, o, o15;//aqui son los 30 cuadros de texto, 15 por nombres y 15 por puntuacion
  2. var z;//var para el boton y que dispare la funcion sortear()
  3. var mayus = new String(a.value, b.value, c.value, d.value, e.value, f.value, g.value, h.value, i.value, j.value, k.value, l.value, m.value, n.value, o.value);
  4.    
  5. function inicio()
  6. {
  7.    
  8.     a = document.getElementById("jugador1");
  9.     a1 = document.getElementById("puntuacion1");
  10.  
  11.     b = document.getElementById("jugador2");
  12.     b2 = document.getElementById("puntuacion2");
  13.  
  14.     c = document.getElementById("jugador3");
  15.     c3 = document.getElementById("puntuacion3");
  16.  
  17.     d = document.getElementById("jugador4");
  18.     d4 = document.getElementById("puntuacion4");
  19.  
  20.     e = document.getElementById("jugador5");
  21.     e5 = document.getElementById("puntuacion5");
  22.  
  23.     f = document.getElementById("jugador6");
  24.     f6 = document.getElementById("puntuacion6");
  25.  
  26.     g = document.getElementById("jugador7");
  27.     g7 = document.getElementById("puntuacion7");
  28.  
  29.     h = document.getElementById("jugador8");
  30.     h8 = document.getElementById("puntuacion8");
  31.  
  32.     i = document.getElementById("jugador9");
  33.     i9 = document.getElementById("puntuacion9");
  34.  
  35.     j = document.getElementById("jugador10");
  36.     j10 = document.getElementById("puntuacion10");
  37.  
  38.     k = document.getElementById("jugador11");
  39.     k11 = document.getElementById("puntuacion11");
  40.  
  41.     l = document.getElementById("jugador12");
  42.     l12 = document.getElementById("puntuacion12");
  43.  
  44.     m = document.getElementById("jugador13");
  45.     m13 = document.getElementById("puntuacion13");
  46.  
  47.     n = document.getElementById("jugador14");
  48.     n14 = document.getElementById("puntuacion14");
  49.  
  50.     o = document.getElementById("jugador15");
  51.     o15 = document.getElementById("puntuacion15");
  52.  
  53.     z = document.getElementById("boton");
  54.     z.addEventListener("click", sortear);
  55.  
  56.  
  57. }
  58.  
  59. function sortn(x,y)
  60. {
  61.     if(x>y)
  62.  
  63.         return -1;
  64.  
  65.     else if(x == y)
  66.    
  67.         return 0;
  68.    
  69.     else
  70.    
  71.         return 1;
  72.    
  73. }
  74.  
  75. function sortear()//y en esta ecuacion me pierdo, pero hice array y empece ciclo
  76. {  
  77.  
  78.     var puntuaciones = new Array(15);
  79.     puntuaciones[0] = a.value.toUpperCase() + " " + new Number(a1.value);
  80.     puntuaciones[1] = b.value.toUpperCase() + " " + new Number(b2.value);
  81.     puntuaciones[2] = c.value.toUpperCase() + " " + new Number(c3.value);
  82.     puntuaciones[3] = d.value.toUpperCase() + " " + new Number(d4.value);
  83.     puntuaciones[4] = e.value.toUpperCase() + " " + new Number(e5.value);
  84.     puntuaciones[5] = f.value.toUpperCase() + " " + new Number(f6.value);
  85.     puntuaciones[6] = g.value.toUpperCase() + " " + new Number(g7.value);
  86.     puntuaciones[7] = h.value.toUpperCase() + " " + new Number(h8.value);
  87.     puntuaciones[8] = i.value.toUpperCase() + " " + new Number(i9.value);
  88.     puntuaciones[9] = j.value.toUpperCase() + " " + new Number(j10.value);
  89.     puntuaciones[10] = k.value.toUpperCase() + " " + new Number(k11.value);
  90.     puntuaciones[11] = l.value.toUpperCase() + " " + new Number(l12.value);
  91.     puntuaciones[12] = m.value.toUpperCase() + " " + new Number(m13.value);
  92.     puntuaciones[13] = n.value.toUpperCase() + " " + new Number(n14.value);
  93.     puntuaciones[14] = o.value.toUpperCase() + " " + new Number(o15.value);
  94.    
  95.     var ganador = puntuaciones.sort(sortn);
  96.     var resultado = document.write(ganador);
  97.    
  98.  
  99.     resultado;
  100.  
  101. }

Aqui os dejo el HTML para que lo podais probar:
Código HTML:
Ver original
  1. <!DOCTYPE HTML>
  2.     <meta charset="utf-8" />
  3.     <title></title>
  4.     <script type="text/javascript" src="sorteo.js">
  5.     </script>
  6. </head>
  7.     <h1>Sorteo</h1>
  8.     <p>Escribe aquí el nombre del jugador:
  9.     <input type="text" id="jugador1"/>
  10.     Escribe aquí la puntuacion obtenida:
  11.     <input type="text" id="puntuacion1"/>
  12.     <br/>
  13.     Escribe aquí el nombre del jugador:
  14.     <input type="text" id="jugador2"/>
  15.     Escribe aquí la puntuacion obtenida:
  16.     <input type="text" id="puntuacion2"/>
  17.     <br/>
  18.     Escribe aquí el nombre del jugador:
  19.     <input type="text" id="jugador3"/>
  20.     Escribe aquí la puntuacion obtenida:
  21.     <input type="text" id="puntuacion3"/>
  22.     <br/>
  23.     Escribe aquí el nombre del jugador:
  24.     <input type="text" id="jugador4"/>
  25.     Escribe aquí la puntuacion obtenida:
  26.     <input type="text" id="puntuacion4"/>
  27.     <br/>
  28.     Escribe aquí el nombre del jugador:
  29.     <input type="text" id="jugador5"/>
  30.     Escribe aquí la puntuacion obtenida:
  31.     <input type="text" id="puntuacion5"/>
  32.     <br/>
  33.     Escribe aquí el nombre del jugador:
  34.     <input type="text" id="jugador6"/>
  35.     Escribe aquí la puntuacion obtenida:
  36.     <input type="text" id="puntuacion6"/>
  37.     <br/>
  38.     Escribe aquí el nombre del jugador:
  39.     <input type="text" id="jugador7"/>
  40.     Escribe aquí la puntuacion obtenida:
  41.     <input type="text" id="puntuacion7"/>
  42.     <br/>
  43.     Escribe aquí el nombre del jugador:
  44.     <input type="text" id="jugador8"/>
  45.     Escribe aquí la puntuacion obtenida:
  46.     <input type="text" id="puntuacion8"/>
  47.     <br/>
  48.     Escribe aquí el nombre del jugador:
  49.     <input type="text" id="jugador9"/>
  50.     Escribe aquí la puntuacion obtenida:
  51.     <input type="text" id="puntuacion9"/>
  52.     <br/>
  53.     Escribe aquí el nombre del jugador:
  54.     <input type="text" id="jugador10"/>
  55.     Escribe aquí la puntuacion obtenida:
  56.     <input type="text" id="puntuacion10"/>
  57.     <br/>
  58.     Escribe aquí el nombre del jugador:
  59.     <input type="text" id="jugador11"/>
  60.     Escribe aquí la puntuacion obtenida:
  61.     <input type="text" id="puntuacion11"/>
  62.     <br/>
  63.     Escribe aquí el nombre del jugador:
  64.     <input type="text" id="jugador12"/>
  65.     Escribe aquí la puntuacion obtenida:
  66.     <input type="text" id="puntuacion12"/>
  67.     <br/>
  68.     Escribe aquí el nombre del jugador:
  69.     <input type="text" id="jugador13"/>
  70.     Escribe aquí la puntuacion obtenida:
  71.     <input type="text" id="puntuacion13"/>
  72.     <br/>
  73.     Escribe aquí el nombre del jugador:
  74.     <input type="text" id="jugador14"/>
  75.     Escribe aquí la puntuacion obtenida:
  76.     <input type="text" id="puntuacion14"/>
  77.     <br/>
  78.     Escribe aquí el nombre del jugador:
  79.     <input type="text" id="jugador15"/>
  80.     Escribe aquí la puntuacion obtenida:
  81.     <input type="text" id="puntuacion15"/>
  82.  
  83.     </p>
  84.     <h2>
  85.         <input type="button" id="boton" value="Aceptar"/>
  86.     </h2>
  87.     <script type="text/javascript">
  88.     inicio();
  89.     </script>
  90. </body>
  91. </html>