mercredi 11 février 2015

Javascript - Troubles with looping through array index


I'm making a quiz that displays how many questions you got right. I have managed to get it to work with the first question but i'm struggling to figure out how to loop through all the other questions within my array.


here's my JS code:



var questionArray = new Array (8);
questionArray [0] = "q1"
questionArray [1] = "q2"
questionArray [2] = "q3"
questionArray [3] = "q4"
questionArray [4] = "q5"
questionArray [5] = "q6"
questionArray [6] = "q7"
questionArray [7] = "q8"



var correctAnswers = 0;

function checkQuestions()
{
var questions = document.getElementsByName(questionArray[0]);
var numberOfRadios = questions.length;

for( var i = 0; i < numberOfRadios; i++)
{
if(questions[i].checked)
{
if(questions[i].value == "correct")
{
correctAnswers++
}
}
}
document.getElementById("correctAnswers").innerHTML = "You got:" + "<br>" + correctAnswers + "/8!!";
}




Aucun commentaire:

Enregistrer un commentaire