I'm learning JS and have to do a trivia game, where each question has 4 possible answers. The questions are given to me in a string with "#", which separates the questions, and "@", which separates each question in its id, its text, and its correct answer.
For the answers I have a string with similar format: idQuestion@idAnswer@textOfAnswer#
I know I did it in a very precarious way (I'm using 4 arrays! ._.), but I can't figure another way to do it.
This is the code where I start from the string which contains the answers, and transform each answer in an "associative array", which is saved in an indexed answer... called assocAnswers
(wtf xD):
var indexAnswers = strAnswers.split("#");
var auxArray = new Array ();
var auxArray2 = new Array();
var assocAnswers = new Array ();
for (var b=0; b<indexAnswers.length; b++){
auxArray = indexAnswers[b].split("@");
auxArray2 = {
"idQuestion":auxArray[0],
"idAnswer":auxArray[1],
"txtAnswer":auxArray[2]
}
assocAnswers.push(auxArray2);
}
I want to know how can I manage to check if the value of idQuestion (which is the first key in each "associative array" that occupies one element of the indexed array) equals x number.
Once I checked that, I have to find the remaining 3 answers with also match the idQuestion, and display the four answers in a div (so I have to display the value of the key txtAnswer
), each answer in a <label>
inside a radio input, but I think I don't need help with the jQuery part. BTW, I haven't learned the for...in loop yet, so if anyone uses it for a possible solution, any explanation of that will be more than welcomed :P Thanks!
Edit: Some examples of the strQuestions in english (strAnswers remains in spanish):
var strQuestions = "1@How is a function without parameters declared?@2#"
+ "2@Which of the next invocations is right?@4#"
+ "3@Given a function: myFunction(a,b) Which is the correct invocation?@3#";
var strAnswers =
"1@1@ function:myFunction()#1@2@ function myFunction()#1@3@ declare myFunction()#1@4@ new myFunction#2@1@Resultado=myFunction#2@2@Call myFunction()#2@3@myFunction#2@4@Resultado=myFunction()#3@1@MiFuncion a,b#3@2@a=MiFuncion(b)#3@3@MiFuncion(x,y)#3@4@Ninguna de las anteriores#