Quiz 1 Source Code
Using a MultiDimensional Array

// the answer arrays are contained in the external answers.js file
<
SCRIPT SRC="answers.js"></SCRIPT>

<SCRIPT>

function score(form) {
     var correct = 0;
     var incorrec = "";

     // answers.length is the number of answers
     for (i = 1; i < answers.
length + 1; i++) {
          var count = 0;
                 var numChecked = 0;

          for (j = 0; j < eval("form.Q" + i).length; j++) {
              var field =
eval("form.Q" + i )[j];

              if (field.checked) {
                    numChecked++;
                   
// Since the answers array is an array of arrays,
    
                 // the individual arrays, in answers, themselves have certain lengths
                     // that correspond to the number of answers for that particular question

                    for (k = 0; k < answers[i - 1].
length; k++) {
         
               // answers is an array of arrays
                          // answers[4 - 1] => a4 => ("a", "d", "e")

     
                     // answers[4 - 1][1] = > a4[1] => "d"
                          if (field.value == answers[i - 1][k]) count++;
               }
          }

          if (numChecked != count) count = -1;
          if (count ==
eval("a" + i).length) correct++;
          else incorrec += "Q" + i + " ";
     }

     form.correct.value = correct;
     form.incorrec.value = incorrec;
}

function showAnswers(form) {
     for (i = 1; i < answers.
length + 1; i++) {
          for (j = 0; j <
eval('form.Q' + i).length; j++) {
              var field =
eval("form.Q" + i )[j];

               field.checked = false;

               for (k = 0; k < answers[i - 1].length; k++) {
                    // answers is an array of arrays
                    // answers[4 - 1] => a4 => ("a", "d", "e")
                    // answers[4 - 1][1] = > a4[1] => "d"

                    if (field.value == answers[i - 1][k]) field.
checked = true
               }
          }
     }

     form.correct.value = "";
     form.incorrec.value = "";
}

</SCRIPT>

CONVENTIONS:

  • Question is 1 is named Q1 & Question 2 is named Q2, etc...
  • Qx are arrays, they use checkboxes or radio buttons
  • The values for the fields are a, b, c, d, e depending on the number fields

<FORM NAME="quiz">
<
OL>
    <
LI>if minutes = 9 & timeSt = ((minutes < 10) ? ":0" : ":") + minutes;
            Then timeSt equals</
LI>
        <
INPUT TYPE="radio" NAME="Q1" VALUE="a"> A. :0<BR>
        <
INPUT TYPE="radio" NAME="Q1" VALUE="b"> B. :09<BR>
        <
INPUT TYPE="radio" NAME="Q1" VALUE="c"> C. :9<BR>
        <
INPUT TYPE="radio" NAME="Q1" VALUE="d"> D. 9</P>

    <LI>The prompt() method requires two pieces of information.
            The first is text to be displayed, and the second is the default data in the entry field.</
LI>
        <
INPUT TYPE="radio" NAME="Q2" VALUE="a"> A. true<BR>
        <
INPUT TYPE="radio" NAME="Q2" VALUE="b"> B. false </P>

etc...

    <LI>Which of the following are legal variable names?</LI>
        <
INPUT TYPE="checkbox" NAME="Q10" VALUE="a">A. _dummy<BR>
        <
INPUT TYPE="checkbox" NAME="Q10" VALUE="b">B. v13<BR>
        <
INPUT TYPE="checkbox" NAME="Q10" VALUE="c">C. 13v<BR>
        <
INPUT TYPE="checkbox" NAME="Q10" VALUE="d">D. A9</P>
</
OL>

You have: <INPUT TYPE="text" NAME="correct"> answers correct out of a possible ten.

The question numbers that were not correct are:
    <
INPUT TYPE="text" NAME="incorrec" SIZE="50">

THE BUTTONS:

  • Get Score - calls the score function which "grades" the quiz. NOTE: it passes the "Entire Form" to function.
  • Show Answers - calls the showAnswers() which displays the correct answer(s) for each question. NOTE: it passes the "Entire Form" to function.

    <INPUT TYPE="button" VALUE="Get Score" onClick="score(this.form)">
    <
INPUT TYPE="button" VALUE="Show Answers" onClick="showAnswers(this.form)">
    <
INPUT TYPE="reset" VALUE="Reset">
</FORM>