[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Converting Javascript Text-Field Quiz to Radio Button Quiz

Posted on 2004-11-09
2
Medium Priority
?
848 Views
Last Modified: 2012-06-27
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
0
Comment
Question by:jeremymgp
2 Comments
 
LVL 63

Accepted Solution

by:
Zvonko earned 2000 total points
ID: 12538297
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
 

Author Comment

by:jeremymgp
ID: 12546885
Hi Zvonko,

That does it! Thx :)

Best regards,

Jeremy
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I've been trying to accomplish this for a while and it just struck me yesterday how to accomplish this task. I have done searches all over the internet looking for ways to email pages from my applications and finally I have done it!!! Every single s…
This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question