Solved

different types of questions in quizzes

Posted on 2004-10-10
3
537 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

In this article, we'll look how to sort an Array in JavaScript, including the more advanced techniques of sorting a collection of records either ascending or descending on two or more fields. Basic Sorting of Arrays First, let's look at the …
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
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

635 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