Solved

different types of questions in quizzes

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

In Part 1 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7849-Hex-Maze.html) we covered the hexagonal maze basics -- how the cells are represented in a JavaScript array and how the maze is displayed.  In this part, we'…
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…

746 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now