Online quiz with radio buttons having same name

Viewed 60

I'm making a simple Quiz using Js, the problem is that my inner loop (i.e i) is not works as expected.

I have taken 3 questions and each question has 3 radio options, options of each question have same name. all the options of fist question have name='cap', options of second question name='an' and third question is name='lang'.

My js function is as follows:

function my(){
  var count=0; 
  var totalQuestions = 3;
  var correctAnswers = 0;
  var alertText;
  var n=["cap","an","lang"];
  var j,i;
  for(j=0; j<n.length; ++j){
    var x = document.getElementsByName('n[j]');
    for(i = 0; i < x.length; ++i){
      if (x[i].checked){
        if (x[i].value == 'true'){
          count=count+10;
          correctAnswers++;
          break;
        }
      }
    }
  }
  if(correctAnswers == totalQuestions){
    alertText = "Congratulations! You got all the questions right!";
  }
  else {
    alertText = "You got " + correctAnswers + " correct answers and score is " + count;
  }
  alert(alertText);
}
3 Answers
Related