Guide to Jean Alvares' Short-Answer Quiz Javascript.  



Below (CLICK  HERE ) is the HTML code for web page containing the Javascript for short-answer  exercise I have created  HERE .This script allows to you to present a set number of questions which ask for a short answer. The script can compare the answer the student types in with one you have previously defined in the script and determine if it is the same, and assign a score. The student's answers can be one letter (which could be good for a multiple-choice type question, one word or a few words long. But remember, the script allows for only one answer, and the longer the answer, the more possibilities  for a typo. This javascript  keeps a running score as the student goes through the quiz.  If you want to see a simple example of such a javascript quiz in action, go  HERE .

To manipulate this javascript, you can copy the HTML code and then save it in a text-only file with a name like MYFILE.HTML, then open it up in a text editor and make  various changes as I describe them below. Remember, when you save this file, it must remain a text-only file, without any italics, boldfacing, or underlining.  You must make sure to make the changes exactly as I specify, or the program will CRASH. 

While one must be careful, it is not too difficult to manipulate this javascript to create different questions and answers. Below are the main areas to be manipulated.


First, how do define or change the initial  questions? Tou This javascript uses arrays, which are lists that contain questions, answers, etc. Thus to change the questions in this javascript, go to the array that contains the questions. Click  HERE  to go to that part of the javascript. See how TEXT OF QUESTION 1. is within quotation marks? Javascript uses quotation marks as part of its basic code for defining text. These boldfaced words and the period can be replaced with whatever text you wish. If the question is long, you may have to adjust the size of the window that the question will appear in. Go  HERE   to see how to do this. However, in writing your question, DO NOT use quotation marks or apostrophes, since Javascript uses these as part of its code, and it may confuse the program. Likewise, do not use italics, underlining or boldfacing. I have only done so to make the text to be substituted clearer.

How do I create the answers to these questions? Again, you go to the array that contains the list of answers. Click  HERE  to go to that section of the javascript. Notice the words, number and period RIGHT ANSWER 1. enclosed in quotation marks. Again, just as in the case of questions, you simply substitute the boldfaced phrase with your chosen correct answer. Remember, the script only can handle EXACT matches. It is probably good somewhere on the page to make sure the students know they have to spell correctly, whether they need to use punctuation, etc.
 

How do I give the students the correct answer to the question or some response to a wrong answer?  The illustration to the left shows you the window and the message it displays if a wrong answer is given. Click  HERE if you wish to go to the section of the   

javascript that produces this message. You could change the phrase Sorry, you are  incorrect. The answer is (which is in quotation marks) to something else, for example If you want to find the correct answer, go to page  The strange bit of code + reference[form.questNo.value] + tells the javascript to go to the third array to get the word or phrase that will be put after the text. Click  HERE  to go to that section of the javascript. Again, as in the previous two arrays, you have a phrase between two quotation marks, which you can alter, here RESPONSE TO ANSWER 1.

What if I want more than three questions? You can add questions, answers and responses easily. All you need to do is to exactly duplicate the form of a question, response or answer, one for each question in each array, and make sure you define the new question as questions[x], rightAns[x] or reference[x], x being the number of the question. Look below left to see the question array in the form I gave it, and to the right see how I have added a fourth question to the question array.
 

questions = new Array() 
 questions[1] = "TEXT OF QUESTION 1. " 
questions[2] = "TEXT OF QUESTION 2. " 
 questions[3] = "TEXT OF QUESTION 3. " 
questions = new Array() 
questions[1] = "TEXT OF QUESTION 1. " 
questions[2] = "TEXT OF QUESTION 2. " 
 questions[3] = "TEXT OF QUESTION 3. " 
questions[4] = "TEXT OF QUESTION 4. " 


You also have to change a bit of the javascript which defines how many questions there are overall.  Click  HERE   to go to that part of the javascript. Note the simple code  var quizEnd = eval(3 * 1);   The 3 here defines the number of questions; change this number to the number of question you create. There must be the same number of questions, answers and responses  program to work. Thus if I had created 25 questions, the command would look like this var quizEnd = eval(25 * 1); 

You can also adjust the size of the boxes the questions, responses and answers appear within in the initial form. For example, to change the size of the box the question is put in, go  HERE to see the code <TEXTAREA COLS=50 ROWS=4 name="question" wrap=virtual></TEXTAREA> By changing the number of COLS the box becomes wider or narrower, and by changing ROWS the height of the box is changed.

If you want to change the size of the box the response appears in, go  HERE , where you see the code <TEXTAREA COLS=50 ROWS=3 name="results" wrap=virtual></TEXTAREA> Again, make the same changes as above.

If you want to change the length of the in which the correct answer is put, go  HERE to see the code <INPUT TYPE=TEXT NAME=yourChoice SIZE=20> . By changing the SIZE the space becomes longer or shorter.

Consider the illustration below. Note the name of the web page "Jean's Test Java Quiz 1". You can change this name by simply going  HERE  and replacing the phrase between <TITLE> and </TITLE>.

If you want to change the text seen below in red (TEST JAVASCRIPT QUIZ 1....)...and the lines below it, go  HERE   and  HERE  and  simply substitute your text for what is between the quotations marks.

These are the basic parts that can be modified. Again, you must be very careful, because the script is unforgiving of errors, misspellings, etc. But, if you experiment, you will soon learn how to easily manipulate this script.



A request. These javascripts are quite simple; so simple, in fact that somebody, like me, who really doesn’t comprehend javascript programming in any detail can manipulate them. However I encourage others who understand these scripts in greater depth to provide suggestions about how they could be improved. What I would really like to be able to figure out is how to add to the third javascript a way of creating a space in which students can add their names and a button that, when pressed, would e-mail the score and the student’s name to a predetermined address. Any suggestions welcome! For comments, corrections, advise, email me at  alvaresj@mail.montclair.edu  Thanks!



   The complete script.  

<HTML>  
<HEAD> 
   <META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1"> 

   <TITLE>Jean's Test Java Quiz 1 </TITLE>  
<SCRIPT LANGUAGE="JavaScript"> 
<!-- hide this script tag's contents from old browsers ---  > 

/*====================================================================== 
   init() 
 
   Description: 
        Initialize the survey form 
 
====================================================================== */ 

function init() { 
   questions = new Array() 
 questions[1] = "TEXT OF QUESTION 1.
questions[2] = "TEXT OF QUESTION 2. " 
 questions[3] = "TEXT OF QUESTION 3. " 

   rightAns = new Array() 
      rightAns[1] = "RIGHT ANSWER 1." 
rightAns[2] = "RIGHT ANSWER 2." 
rightAns[3] = "RIGHT ANSWER 3." 
 

   reference = new Array() 
     reference[1] = "RESPONSE TO ANSWER 1."  
reference[2] = "RESPONSE TO ANSWER 2." 
reference[3] = "RESPONSE TO ANSWER 3." 
 

/*=========================================================== 
   nextQuestion(form) 
 
   Description: 
        Display the next question 
======================================================== */ 
function nextQuestion(form) { 
//set the total number of questions equal to quizend 
   var quizEnd = eval(3 * 1);  
   if(form.questNo.value == quizEnd) { 
       form.question.value = ""; 

       form.yourChoice.value = ""; 
       form.results.value = "End of Quiz.  Your final results are listed below."; } else { 

    if(form.questNo.value == "") {form.questNo.value = 1} else { 
       form.questNo.value = eval(form.questNo.value) + 1; 
    } 

    form.question.value = questions[form.questNo.value]; 
 

    form.yourChoice.value = ""; 
    form.results.value = ""; 

    if(form.myScore.value == "") {form.myScore.value = 0; } else { 
                                                  form.myScore.value = form.myScore.value; } 
   } 

/*====================================================================== 
   checkAnswer(form) 
 
   Description: 
        Check the quiz answers 
 
====================================================================== */ 
function checkAnswer(form) { 
      var myScore = 0; 
      var curve = 0; 

      if(form.results.value != "") {form.results.value = "Sorry, no guessing allowed.  In order to retry this question you will have to start the quiz over.  Click on ''Next Question'' to continue."; } 

      else if(form.yourChoice.value == rightAns[form.questNo.value]) { 
      form.myScore.value = eval(form.myScore.value) + eval(1); 
      form.results.value = "Congratulations!  You are correct. That brings your cumulative score to " + form.myScore.value + " out of a possible " + form.questNo.value + ".  Click on ''Next Question'' to continue."; } else { 
 
      form.results.value = "Sorry, you are  incorrect. The answer is " + reference[form.questNo.value] + ".  This brings your cumulative score to " + form.myScore.value + " out of a possible " + form.questNo.value + ".  Click on ''Next Question'' to continue." } 
 
   curve = form.myScore.value / form.questNo.value; 
   form.percent.value = parseInt(curve * 100,10); 
 
   if(curve > .90) {form.grade.value = "A"; } else if(curve > .80) {form.grade.value = "B"; } else if(curve > .70) {form.grade.value = "C"; } else if(curve > .60) {form.grade.value = "D"; } else {form.grade.value = "F"; } 

/*====================================================================== 
   clearForm(form) 
 
   Description: 
        Clear all fields within the form 
 
====================================================================== */ 
function clearForm(form) { 
    form.questNo.value = ""; 
    form.question.value = ""; 

    form.yourChoice.value = ""; 
    form.results.value = ""; 
    form.myScore.value = ""; 
    form.percent.value = ""; 
    form.grade.value = ""; 

<!-- done hiding from old browsers --> 
</SCRIPT> 
</HEAD> 
<BODY TEXT="#4C0000" BGCOLOR="#FFFFFF" LINK="#0000FF" VLINK="#551A8B" ALINK="#0000FF" onLoad="init()"> 

<CENTER> 
 

<P><FORM method=POST> 
<CENTER><TABLE BORDER BGCOLOR="#FFFFFF" > 
<TR> 
<TD COLSPAN="2"> 
<CENTER><FONT FACE="Palatino"><FONT COLOR="#ED181E"><FONT SIZE=+2>TEST JAVASCRIPT QUIZ 1</FONT></FONT></FONT></CENTER> 
</TD> 
</TR> 

<TR> 
<TD COLSPAN="2" BGCOLOR="#FFFFFF"> 
<CENTER><FONT COLOR="#ED181E"><FONT SIZE=+1>Click on "Next Question" to 
start quiz. Be sure to use small letters (no caps) writing your answer. Also, in order to get credit for each right answer, be sure to click on "Check Current Answer" before going on to the next question. You can edit this space as you wish. </FONT></FONT></CENTER> 
</TD> 
</TR> 
<TR> 
<TD><B>Question #:</B></TD> 

<TD ALIGN=LEFT><INPUT TYPE=TEXT NAME=questNo SIZE=4></TD> 
</TR> 

<TR> 
<TD><B>Question:</B></TD> 

<TD> 
<CENTER> 
<TEXTAREA COLS=50 ROWS=4 name="question" wrap=virtual></TEXTAREA>  
</CENTER> 
</TD> 
</TR> 
<TR> 
<TD ALIGN=RIGHT><B>Your choice:</B></TD> 

<TD><INPUT TYPE=TEXT NAME=yourChoice SIZE=20></TD>  
</TR> 

<TR> 
<TD COLSPAN="2"> 
<CENTER> 

<INPUT TYPE="button" VALUE="Check Current Answer" onClick="checkAnswer(this.form)"> 
<INPUT TYPE="button" VALUE="Next Question" onClick="nextQuestion(this.form)"> 
<INPUT TYPE="reset"  VALUE="Start Over" onClick="clearForm(this.form)"> 
</CENTER> 
</TD> 
</TR> 

<TR> 
<TD ALIGN=RIGHT><B>Results:</B></TD> 

<TD> 
<CENTER> 
<TEXTAREA COLS=50 ROWS=3 name="results" wrap=virtual></TEXTAREA> 
</CENTER> 
</TD> 
</TR> 

<TR> 
<TD ALIGN=RIGHT><B>Current Score:</B></TD> 

<TD> 
<B>Points:<INPUT TYPE=TEXT NAME=myScore SIZE=4></B>&nbsp; 
<B>Percentage:<INPUT TYPE=TEXT NAME=percent SIZE=6></B>&nbsp; 
<B>Letter Grade:</B><INPUT TYPE=TEXT NAME=grade SIZE=6> 
</TD> 
</TR> 
</TABLE></CENTER> 
</FORM> 
<BR> 
</BODY> 
</HTML> 
 

  Copyright April 1999 Jean Alvares
 
  Hit Counter visits since 16 April 1999.