Solved

different types of questions in quizzes

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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'…
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
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…

734 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