Link to home
Start Free TrialLog in
Avatar of jeremymgp
jeremymgp

asked on

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
ASKER CERTIFIED SOLUTION
Avatar of Zvonko
Zvonko
Flag of North Macedonia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of jeremymgp
jeremymgp

ASKER

Hi Zvonko,

That does it! Thx :)

Best regards,

Jeremy