Converting Javascript Text-Field Quiz to Radio Button Quiz

Hi,

I'm using a simple quiz script with text-fields, if the user's answer matches an array of answers they get scored appropriately.

I'd like to use this same system, but with radio buttons instead. Users click on a multiple-choice answer radio button, if it matches the corresponding item in an array of answers (answer "a"/"b"/"c" for example) they get scored appropriately. Here's my script using text-fields:

<SCRIPT language=JavaScript>
<!--
var numQues = 10;
var answers = new Array(10);
answers[0] = "a";
answers[1] = "b";
answers[2] = "a";
answers[3] = "a";
answers[4] = "b";
answers[5] = "b";
answers[6] = "a";
answers[7] = "a";
answers[8] = "b";
answers[9] = "b";

function getScore(form) {
  var score = 0;

  for (i=0; i<numQues; i++) {
    if (form.elements[i].value == answers[i]) {
      score++;
    }
  }

  score = Math.round(score/numQues*100);
  form.percentage.value = score + "%";
  var correctAnswers = "";
  for (i=1; i<=numQues; i++) {
    correctAnswers += i + ". " + answers[i-1] + "\r\n";
  }
  form.solutions.value = correctAnswers;
}

// -->
</SCRIPT>

The HTML form goes like this:
<FORM name=quiz>
<input type="text" name="q1" size="25">
<input type="text" name="q2" size="25">
<input type="text" name="q3" size="25">

... and so on up to question 10 ...

<INPUT size=5 name=percentage>
<TEXTAREA name=solutions rows=13 wrap=virtual cols=30></TEXTAREA>
<input onClick=getScore(this.form) type=button value="Grade Me" name="button">
<INPUT type=reset value=Clear>
</FORM>

Please provide the script to have this quiz work with multiple-choice radio buttons with answers a, b, c. Thankyou!

Best Regards,

Jeremy
jeremymgpAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ZvonkoSystems architectCommented:
Here an example:

<SCRIPT language=JavaScript>
<!--
var answers = ["a", "b", "a", "a", "b", "b", "a", "a", "b", "b"];
var numQues = answers.length;
function getScore(theBtn) {
  theForm = theBtn.form;
  var score = 0;
  for(var i=0; i<numQues; i++) {
    var q = theForm["q"+(i+1)];
    qVal = "";
    for(var j=0;j<q.length;j++){
      if(q[j].checked==true){
        qVal = q[j].value;
      }
    }
    if (qVal == answers[i]) {
      score++;
    }
  }

  score = Math.round(score/numQues*100);
  theForm.percentage.value = score + "%";
  var correctAnswers = "";
  for (var i=1; i<=numQues; i++) {
    correctAnswers += i + ". " + answers[i-1] + "\r\n";
  }
  theForm.solutions.value = correctAnswers;
}

// -->
</SCRIPT>

<FORM name=quiz>
<input type="radio" name="q1" value="a"> a
<input type="radio" name="q1" value="b"> b
<input type="radio" name="q1" value="c"> c<br>
<input type="radio" name="q2" value="a"> a
<input type="radio" name="q2" value="b"> b
<input type="radio" name="q2" value="c"> c<br>
<input type="radio" name="q3" value="a"> a
<input type="radio" name="q3" value="b"> b
<input type="radio" name="q3" value="c"> c<br>
<input type="radio" name="q4" value="a"> a
<input type="radio" name="q4" value="b"> b
<input type="radio" name="q4" value="c"> c<br>
<input type="radio" name="q5" value="a"> a
<input type="radio" name="q5" value="b"> b
<input type="radio" name="q5" value="c"> c<br>
<input type="radio" name="q6" value="a"> a
<input type="radio" name="q6" value="b"> b
<input type="radio" name="q6" value="c"> c<br>
<input type="radio" name="q7" value="a"> a
<input type="radio" name="q7" value="b"> b
<input type="radio" name="q7" value="c"> c<br>
<input type="radio" name="q8" value="a"> a
<input type="radio" name="q8" value="b"> b
<input type="radio" name="q8" value="c"> c<br>
<input type="radio" name="q9" value="a"> a
<input type="radio" name="q9" value="b"> b
<input type="radio" name="q9" value="c"> c<br>
<input type="radio" name="q10" value="a"> a
<input type="radio" name="q10" value="b"> b
<input type="radio" name="q10" value="c"> c<br>

<INPUT size=5 name=percentage><br>
<TEXTAREA name=solutions rows=13 wrap=virtual cols=30></TEXTAREA><br>
<input onClick=getScore(this) type=button value="Grade Me" name="button">
<INPUT type=reset value=Clear>
</FORM>


0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
jeremymgpAuthor Commented:
Hi Zvonko,

That does it! Thx :)

Best regards,

Jeremy
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.