Home Articles FAQs XREF Games Software Instant Books BBS About FOLDOC RFCs Feedback Sitemap
irt.Org
#

Q97 How can I do a multiple choice quiz?

You are here: irt.org | FAQ | JavaScript | Misc | Q97 [ previous next ]

<script language="JavaScript"><!--
var alphabet = 'ABCD';

function showQuestion(question,answer,clue,choice1,choice2,choice3,choice4) {
    var output = '<p><b>Question ' + (numberOfQuestions + 1) + ':<\/b> ' + question + '<p>';

    for (var i = 3, j = 1; i<showQuestion.arguments.length; i++, j++) {
        output += alphabet.substring(j-1,j) + '. ';
        if (answer == j) output += '<input type="radio" name="q' + numberOfQuestions + '" value="true">';
        else             output += '<input type="radio" name="q' + numberOfQuestions + '" value="false">';
        output += ' ' + showQuestion.arguments[i] + '<br>';
    }

    output += '<p><input type="button" value="Clue" onClick="alert(\'' + clue + '\')">';

    numberOfQuestions++;

    return output;
}

function validateAnswers() {
    var correct = 0, wrong = 0, blank = 0;
    for (var i=0;i<numberOfQuestions;i++) {
        var CA = eval("document.quizForm.q" + i + "[0].checked");
        var CB = eval("document.quizForm.q" + i + "[1].checked");
        var CC = eval("document.quizForm.q" + i + "[2].checked");
        var CD = eval("document.quizForm.q" + i + "[3].checked");
        var AA = eval("document.quizForm.q" + i + "[0].value");
        var AB = eval("document.quizForm.q" + i + "[1].value");
        var AC = eval("document.quizForm.q" + i + "[2].value");
        var AD = eval("document.quizForm.q" + i + "[3].value");

        if (CA == CB == CC == CD) blank++; // can't all be checked, thus must be all unchecked
        else
            if ((CA.toString() == AA) && (CB.toString() == AB) && (CC.toString() == AC) && (CD.toString() == AD))
                correct++;
            else wrong++;
    }
    document.quizForm.correct.value = correct;
    document.quizForm.wrong.value = wrong;
    document.quizForm.blank.value = blank;

    if (correct != numberOfQuestions) return false; else return true;
}

var numberOfQuestions = 0;
var o = '<form name="quizForm" action"mailto:someone@somewhere.com" method="post" enctype="text/plain" onSubmit="return validateAnswers()">';

o += showQuestion('Is black = white?',1,'Are you kidding me? Do you really want a clue?','No','Yes','Sometimes','All of the above');
o += showQuestion('Whats the square root of 36?',1,'What number multipled by itself equals 36?','6','36','30','12');
o += showQuestion('Whats the opposite of neutral?',3,'Check the dictionary definition of each word','Impartial','Partial','Partisan','All of the above');
o += showQuestion('In Bosnia-Herzegovina what is the currency unit?',1,'Tough one this','dinar = 100 paras','dinar = 1,000 fils','dinar = 10 pounds','dinar = 1,000 milliemes')
o += showQuestion('What is the definition of dyslexia?',4,'Based on Greek lexis (speech) and latin legree (to read)',
     'Impaired nourishment of an organ or part of the body',
     'Lack of coordination in speech, owing to brain damage',
     'Indigestion',
     'A disorder marked especially by severe difficulty in reading and spelling');

o += '<p><input type="submit" value="Submit for grading">';
o += ' Correct: <input type="textbox" name="correct" value="" size="2">';
o += ' Wrong: <input type="textbox" name="wrong" value="" size="2">';
o += ' Blank: <input type="textbox" name="blank" value="" size="2">';


o += '<\/form>';

document.write(o);
//--></script>

Feedback on 'Q97 How can I do a multiple choice quiz?'

©2018 Martin Webb