Solved

different types of questions in quizzes

Posted on 2004-10-10
3
535 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 250 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 250 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Javascript object functions help 1 18
Date on a table 16 33
Hta File displays dynamic File names 4 43
Prevent certain words from being typed in a form 6 24
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…
The task A number given should be formatted for easy reading by separating digits into triads. Format must be made inline via JavaScript, i.e., frameworks / functions are not welcome. So let’s take a number like this “12345678.91¿ and format i…
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…

685 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