Quiz Source Code

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

<SCRIPT>

// "scores" the quiz
function score(form) {
     var correct = 0;
// number of correct answers
     var incorrec = "";
// what questions are incorrect or not answered

     // Questions - how questions are there, this comes from the ans.js file
     for (i = 1; i < Questions + 1; i++) {
          var count = 0;
//counter used to determine if a particular question was answered correctly
                var numChecked = 0; //counter used to verify that only a valid element is checked
          // we need to determine the number of "fields" for each question,
          // since this number is a variable
          // (some have 2 "fields" such as true & false & others have 4 or 5 "fields")
          // eval converts a string variable into a Form "Question" Object
          // the Form "Question" Object is an array &
          // we need to evaluate each individual element in the array

          for (j = 0; j < eval("form.Q" + i).length; j++) {
               var field = eval("form.Q" + i)[j];
// shortcut to a particular Form "Question"
              if (field.checked) {
                    numChecked++;
//counter for the number elements checked
                    
// eval converts a variable string into an Answer Array Object
                     // that corresponds to that particular question
                     // we need to loop through the respective Answer Array
                     // in order to check if the answers match with those "fields" that are checked

                     // eval("a" + i) is the answer array for that particular question
                     // and length corresponds to number of correct answers for that particular question

                     for (k = 0; k < eval("a" + i).length; k++) {
                          // we need to check for two conditions:
                          // 1. is the field checked &
                          // 2. if it is checked does the value match one of the answers
                          // in it's corresponding Answer Array
                         // eval converts a variable string into an Answer Array Object

                    if (field.value == eval("a" + i)[k]) count++;
               }
          }
          // if these numbers do not match
          // then an element was checked that shouldn't have been checked

          // set the count to -1 in order to have answer marked as incorrect
          if (numChecked != count) count = -1;

          // if the count equals the number of elements in the respective Answer Array then
          // the User answered the entire question correctly

          if (count == eval("a" + i).length) correct++;
          // else the User answered the question incorrectly or left the question "blank"
          else incorrec += "Q" + i + " ";
     }

     form.correct.value = correct; // display the number of correct answers
     form.incorrec.value = incorrec;
// display which answers were incorrect or left "blank"
}

// display all the correct answers for the Quiz
function showAnswers(form) {
     // Questions - how questions are there, this comes from the ans.js file
     for (i = 1; i < Questions + 1; i++) {
          var field = eval("form.Q" + i )[j];
// shortcut to a particular Form "Question"
          field.checked = false;
// uncheck all the elements - start out with a clean slate
          // we need to determine the number of "fields" for each question,
          // since this number is a variable
          // (some have 2 "fields" such as true & false & others have 4 or 5 "fields")
          // eval converts a string variable into a Form "Question" Object
          // the Form "Question" Object is an array &
          // we need to evaluate each individual element in the array

          for (j = 0; j < eval("form.Q" + i).length; j++) {
               // eval converts a variable string into an Answer Array Object
               // that corresponds to that particular question
               // we need to loop through the respective Answer Array
               // in order to check if the answers match with those "fields" that are checked

               for (k = 0; k < eval("a" + i).length; k++) {
                    // if the "field" value matches to one of those in the Answer Array
                    // then check that respective "field"

                    if (field.value == eval("a" + i)[k]) field.checked = true;
               }
          }
     }

     form.correct.value = ""; // empty the correct text field
     form.incorrec.value = "";
// empty the incorrect text field
}

</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>