Foro de elhacker.net

Programación => Desarrollo Web => Mensaje iniciado por: Bard44 en 3 Enero 2018, 05:49 am



Título: Problema con Cuestionario en javascript
Publicado por: Bard44 en 3 Enero 2018, 05:49 am
Buenas Noches, tengo un problema con un cuestionario que estoy realizando en javascript, el problema es que tengo 10 preguntas de selección única usando un input type=radio, con 4 respuestas a escoger, esas 10 si funcionan perfectamente y van sumando para al final desplegar una nota, pero al agregar una pregunta de respuesta corta usando un input type=text que sería la 11 esa no funciona, de hecho al dar el botón de= Responder, toma la respuesta como incorrecta y lo que quiero lograr es que siga sumando esa respuesta obviamente si es correcta con el resto pero no se donde estoy fallando, les agradecería la ayuda, muchas gracias.

Este es el código en html:

Código
  1. <!doctype html>
  2. <link rel="stylesheet" type="text/css" href="css/estilo.css">
  3. <script src="js/Proyecto.js"></script>
  4.  
  5. </head>
  6. <header class="div1">
  7. <p class="p1">CUESTIONARIO</p></header>
  8.  
  9.  
  10. <label class="label">Pregunta 1</label>
  11.  
  12.  
  13. <input type=radio value="a" onClick="Engine(1, this.value)">Rehacer, reciclar, reponer
  14.  
  15. <input type=radio value="b" onClick="Engine(1, this.value)">Reutilizar, reciclar, reducir
  16.  
  17. <input type=radio value="c" onClick="Engine(1, this.value)">Reducir, recoger, renacer
  18.  
  19. <input type=radio value="d" onClick="Engine(1, this.value)">Rehacer, renovar, retornar<p>
  20.  
  21. <label class="label">Pregunta 2</label>
  22.  
  23.  
  24. <input type=radio value="a" onClick="Engine(2, this.value)">10 años
  25.  
  26. <input type=radio value="b" onClick="Engine(2, this.value)">20 años
  27.  
  28. <input type=radio value="c" onClick="Engine(2, this.value)">700 años
  29.  
  30. <input type=radio value="d" onClick="Engine(2, this.value)">200 años<p>
  31.  
  32. <label class="label">Pregunta 3</label>
  33.  
  34.  
  35. <input type=radio value="a" onClick="Engine(3, this.value)">500 años
  36.  
  37. <input type=radio value="b" onClick="Engine(3, this.value)">1000 años
  38.  
  39. <input type=radio value="c" onClick="Engine(3, this.value)">5 años
  40.  
  41. <input type=radio value="d" onClick="Engine(3, this.value)">300 años<p>
  42.  
  43. <label class="label">Pregunta 4</label>
  44.  
  45.  
  46. <input type=radio value="a" onClick="Engine(4, this.value)">450 años
  47.  
  48. <input type=radio value="b" onClick="Engine(4, this.value)">10 años
  49.  
  50. <input type=radio value="c" onClick="Engine(4, this.value)">500 años
  51.  
  52. <input type=radio value="d" onClick="Engine(4, this.value)">80 años<p>
  53.  
  54. <label class="label">Pregunta 5</label>
  55.  
  56.  
  57. <input type=radio value="a" onClick="Engine(5, this.value)">4 años
  58.  
  59. <input type=radio value="b" onClick="Engine(5, this.value)">100 años
  60.  
  61. <input type=radio value="c" onClick="Engine(5, this.value)">Nunca
  62.  
  63. <input type=radio value="d" onClick="Engine(5, this.value)">350 años<p>
  64.  
  65. <label class="label">Pregunta 6</label>
  66.  
  67.  
  68. <input type=radio value="a" onClick="Engine(6, this.value)">5
  69.  
  70. <input type=radio value="b" onClick="Engine(6, this.value)">4
  71.  
  72. <input type=radio value="c" onClick="Engine(6, this.value)">2
  73.  
  74. <input type=radio value="d" onClick="Engine(6, this.value)">10<p>
  75.  
  76. <label class="label">Pregunta 7</label>
  77.  
  78.  
  79. <input type=radio value="a" onClick="Engine(7, this.value)">Rojo
  80.  
  81. <input type=radio value="b" onClick="Engine(7, this.value)">Negro
  82.  
  83. <input type=radio value="c" onClick="Engine(7, this.value)">Azul
  84.  
  85. <input type=radio value="d" onClick="Engine(7, this.value)">Verde<p>
  86.  
  87. <label class="label">Pregunta 8</label>
  88.  
  89.  
  90. <input type=radio value="a" onClick="Engine(8, this.value)">Amarillo
  91.  
  92. <input type=radio value="b" onClick="Engine(8, this.value)">Verde
  93.  
  94. <input type=radio value="c" onClick="Engine(8, this.value)">Azul
  95.  
  96. <input type=radio value="d" onClick="Engine(8, this.value)">Negro<p>
  97.  
  98. <label class="label">Pregunta 9</label>
  99.  
  100.  
  101. <input type=radio value="a" onClick="Engine(9, this.value)">Plástico
  102.  
  103. <input type=radio value="b" onClick="Engine(9, this.value)">Vidrio
  104.  
  105. <input type=radio value="c" onClick="Engine(9, this.value)">Restos de verduras y frutas
  106.  
  107. <input type=radio value="d" onClick="Engine(9, this.value)">Restos de carne<p>
  108.  
  109. <label class="label">Pregunta 10</label>
  110.  
  111.  
  112. <input type=radio value="a" onClick="Engine(10, this.value)">Rojo
  113.  
  114. <input type=radio value="b" onClick="Engine(10, this.value)">Verde
  115.  
  116. <input type=radio value="c" onClick="Engine(10, this.value)">Amarillo
  117.  
  118. <input type=radio value="d" onClick="Engine(10, this.value)">Rosado<p>
  119.  
  120. <label class="label">11.    Escriba un ejemplo de papel que se puede reciclar: </label>
  121.     <input class="input2" type="text"><button type=button onClick="Engine(11, this.value)">Responder</button>
  122. <input type=button onClick="java()" value="Terminar">
  123. </form>
  124. <footer>
  125. </footer>
  126. </body>
  127. </html>

Este es el código en javascript:

Código
  1. var ans = new Array;
  2. var done = new Array;
  3. var score = 0;
  4.  
  5. ans[1] = "b";
  6. ans[2] = "c";
  7. ans[3] = "a";
  8. ans[4] = "d";
  9. ans[5] = "c";
  10. ans[6] = "b";
  11. ans[7] = "b";
  12. ans[8] = "b";
  13. ans[9] = "c";
  14. ans[10] = "c";
  15. ans[11] = "papel";
  16.  
  17. function Engine(question, answer) {
  18.  
  19.    if (answer != ans[question]) {
  20.        if (!done[question]) {
  21.            done[question] = -1;
  22.            alert("Error!\n\nTu puntaje por el momento es: " + score);
  23.                }
  24.        else {
  25.            alert("Ya respondiste a esta pregunta!");
  26.            }
  27.        }
  28.    else {
  29.        if (!done[question]) {
  30.            done[question] = -1;
  31.            score++;
  32.            alert("Respuesta Correcta!\n\nTu puntaje por el momento es: " + score);
  33.                }
  34.        else {        
  35.            alert("Ya respondiste a esta pregunta");
  36.            }
  37.        }
  38. }
  39.  
  40. function java () {
  41.    if (score == 11) {
  42.        alert("nota: 100");
  43.        }
  44.    if (score == 9) {
  45.        alert("nota: 90");
  46.        }
  47.    if (score == 8) {
  48.        alert("nota: 80");
  49.        }
  50.    if (score == 7) {
  51.        alert("nota: 70");
  52.        }
  53.    if (score == 6) {
  54.        alert("nota: 60");
  55.        }
  56.    if (score == 5) {
  57.        alert("nota: 50");
  58.        }
  59.    if (score == 4) {
  60.        alert("nota: 40");
  61.        }
  62.    if (score == 3) {
  63.        alert("nota: 30");
  64.        }
  65.    if (score == 2) {
  66.        alert("nota: 20");
  67.        }
  68.    if (score == 1) {
  69.        alert("nota: 10");
  70.        }
  71.    if (score === 0) {
  72.        alert("nota: 0");
  73.        }
  74.    if (score >= 7 &amp;&amp; score <= 12) {
  75.        alert("Acceso permitido")
  76.        }
  77.    else {
  78.        alert("No puedes seguir, necesitas al menos 7 buenas para continuar")
  79.        }
  80. }



Título: Re: Problema con Cuestionario en javascript
Publicado por: Juan Enrique en 30 Enero 2018, 17:58 pm
Hola, creo que el problemas esta en que mandas el valor que tiene el botón y no el texto.

Código:
<input class="input2" type="text"><button type=button onClick="Engine(11, this.value)">Responder</button>

En ese trozo, creo que tendría que ser algo así en caso de respetar tu código y no modificarlo mucho:

Código:
<input class="input2" type="text"><button value="papel" type=button onClick="Engine(11, this.value)">Responder</button>

No se si llego tarde pero espero que te ayude  :D