Questions and answers Javascript

0

Trying to make a small trivial in JS, I can not solve the error of the if or the arrays. In this case the correct answer would be b, which implies that typing something else is false. What should I modify in this case?

var preguntas= new Array();
    
var respuesta=new Array();

preguntas[0]=new Array();

preguntas[0][0]= prompt("¿Cuánto mide el Everest? \n a: 8.912 m \n b:8.848 m \n c:8.800 m");
preguntas[0][1]="8.912";
preguntas[0][2]="8.848";
preguntas[0][3]="8.800";

respuesta[2]="b";

if(respuesta[2]=="b") {
  alert("Acierto");
}else {
  alert("Fallo");
}
    
asked by user09b 28.09.2018 в 10:17
source

1 answer

2

The fact is that you always indicate that the solution regardless of what the user says is b in the following line:

respuesta[2]="b";

You are saving in the variable preguntas[0][0] what the user answers, because you have to save that result in the answer.

var preguntas= new Array();
    
var respuesta=new Array();

preguntas[0]=new Array();

preguntas[0][0]= prompt("¿Cuánto mide el Everest? \n a: 8.912 m \n b:8.848 m \n c:8.800 m");
preguntas[0][1]="8.912";
preguntas[0][2]="8.848";
preguntas[0][3]="8.800";

respuesta[2]=preguntas[0][0];

if(respuesta[2]=="b") {
  alert("Acierto");
}else {
  alert("Fallo");
}

And if you want to save the answer in the variable respuesta then change the if of the comparison to this:

var preguntas= new Array();
    
var respuesta=new Array();

preguntas[0]=new Array();

preguntas[0][0]= prompt("¿Cuánto mide el Everest? \n a: 8.912 m \n b:8.848 m \n c:8.800 m");
preguntas[0][1]="8.912";
preguntas[0][2]="8.848";
preguntas[0][3]="8.800";

respuesta[2]="b";

if(respuesta[2]==preguntas[0][0]) {
  alert("Acierto");
}else {
  alert("Fallo");
}
    
answered by 28.09.2018 в 14:11