Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

different types of questions in quizzes

Posted on 2004-10-10
3
Medium Priority
?
539 Views
Last Modified: 2008-03-17

Hi everyone,

I am designing a quiz using javascripts.

I have this portion of code to determine if the answer is right or wrong,

but the problem is that this code works for multiple choices questions only

I want to use this code for other tupes of question such as true & false and word completion

could anyone please help me with this

----------

var numQues = 3;
var numChoi = 4;


var answers = new Array(3);
answers[0] = "John";
answers[1] = "1995";
answers[2] = "1966";



function getScore(form) {
  var score = 0;
  var currElt;
  var currSelection;


  for (i=0; i<numQues; i++) {
    currElt = i*numChoi;
    for (j=0; j<numChoi; j++) {
      currSelection = form.elements[currElt + j];
      if (currSelection.checked) {
        if (currSelection.value == answers[i]) {
          score++;
          break;
        }
      }
    }
  }


  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;


}

---------
0
Comment
Question by:swet
  • 2
3 Comments
 
LVL 6

Assisted Solution

by:sathishv
sathishv earned 1000 total points
ID: 12274458
For True/False, you can have a radio button in your form,

<input type="radio" name="r1" value="true">True
<input type="radio" name="r1" value="false">False

Your multiple selection script should handle the radio buttons also.

For word completion answers, slightly modify the 'for' loop,

for (i=0; i<numQues; i++) {
    currElt = i*numChoi;
    for (j=0; j<numChoi; j++) {
      currSelection = form.elements[currElt + j];
      if (currSelection.type == "text" || currSelection.checked) {   //in case a textbox is provided, it directly compares the value
        if (currSelection.value == answers[i]) {
          score++;
          break;
        }
      }
    }
  }

Cheers,
Sathish
0
 

Author Comment

by:swet
ID: 12283980

thanks Sathish


I still have a problem in this line  

the complaint is about the word 'type' ..  I am using a textfield but Im afraid I don't understand your comment

----------

 if (currSelection.type == "text" || currSelection.checked) {   //in case a textbox is provided, it directly compares the value
0
 
LVL 6

Accepted Solution

by:
sathishv earned 1000 total points
ID: 12295479
The 'type' property gives the control type you are using (eg: textbox, radio button etc.). I have used it in IE6 and NN7, but haven't tried in earlier versions or other browsers.

The condition in the if block: if the control is a textbox, it returns true for type part and false for checked part, if it is a checkbox or radio button vice versa.

Otherwise you might want to expand it like this

if (currSelection.type == "text")
{
        if (currSelection.value == answers[i]) {
          score++;
          break;
        }
}
else if((currSelection.type == "radio" || currSelection.type == "checkbox") && currSelection.checked) {
        if (currSelection.value == answers[i]) {
          score++;
          break;
        }
      }
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

A while back, I ran into a situation where I was trying to use the calculated columns feature in SharePoint 2013 to do some simple math using values in two lists. Between certain data types not being accessible, and also with trying to make a one to…
In this blog, we’ll look at how improvements to Percona XtraDB Cluster improved IST performance.
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

885 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