Test Given and Graded

[Total: 0    Average: 0/5]

This shows how to have a test given in javascript.

Head Code:

<script>
var all_Questions = new Array();

/*
 * Construct a_Question:
 *    TYPE = "TF","MC","FB" (True/False, Multiple-Choice, Fill-In-The-Blank)
 *    X    = true(1)/false(0) (true means this question is extra credit)
 *    Q    = Question
 *    CA   = Correct Answer (1,2,3,4)
 *    A1   = Possible Answer #1
 *    A2   = Possible Answer #2 (not manditory)
 *    A3   = Possible Answer #3 (not manditory)
 *    A4   = Possible Answer #4 (not manditory)
 */
function a_Question (_type,_X,_Q,_CA,_A1,_A2,_A3,_A4)
{
  this.TYPE = _type;
  this.EXTRA = _X;
  this.QUESTION = _Q;
  this.ANSWERS = new Array();
  if(_A1)                      this.ANSWERS[this.ANSWERS.length?this.ANSWERS.length:0]=_A1;
  if(_A1 && _A2)               this.ANSWERS[this.ANSWERS.length?this.ANSWERS.length:0]=_A2;
  if(_A1 && _A2 && _A3)        this.ANSWERS[this.ANSWERS.length?this.ANSWERS.length:0]=_A3;
  if(_A1 && _A2 && _A3 && _A4) this.ANSWERS[this.ANSWERS.length?this.ANSWERS.length:0]=_A4;
  this.CORRECT_ANSWER = -1+_CA;
  if(this.CORRECT_ANSWER >= this.ANSWERS.length)
    alert("This test question has an answer that is not in the list.nQuestion:n"+this.QUESTION);
}
/*
 * END OF CONSTRUCT DEFINITION
 */

function add_Q(_t,_x,_q,_ca,_a1,_a2,_a3,_a4){all_Questions[all_Questions.length?all_Questions.length:0]=new a_Question(_t,_x,_q,_ca,_a1,_a2,_a3,_a4);}
function show_Questions()
{
  var _FIRST_MC=false;
  var _FIRST_FB=false;
  var _FIRST_TF=false;
  var _FIRST_XC=false;
  document.write("<h2>Please answer each test question, then  click 'Get score.' Your test score will be displayed with the correct answers shown.<BR>The section below contains mandatory questions. All questions MUST BE ANSWERED correctly in order to pass this test. No exceptions!</h2><BR>");

  for(var i=0;i<all_Questions.length;i++)
  {
    var _msg ="";
	if(!_FIRST_XC && all_Questions[i].EXTRA)
	{
	  _FIRST_XC=true;
	  _msg+="<h2>Extra Credit Questions</h2><BR>";
	}
	if(all_Questions[i].TYPE=="MC")
	{
	    if(!_FIRST_MC)
		{
		  _FIRST_MC=true;
		  _msg+="<h2>Multiple Choice Questions</h2><BR>";
		}
	    _msg+="<h4>"+(i+1)+".<font color=red>"+all_Questions[i].QUESTION+"</font></h4>";
		_msg+="<input type=radio name='q"+(1+i)+"' value=A>A. "+all_Questions[i].ANSWERS[0]+"<BR>";
		_msg+="<input type=radio name='q"+(1+i)+"' value=B>B. "+all_Questions[i].ANSWERS[1]+"<BR>";
		_msg+="<input type=radio name='q"+(1+i)+"' value=C>C. "+all_Questions[i].ANSWERS[2]+"<BR>";
		_msg+="<input type=radio name='q"+(1+i)+"' value=D>D. "+all_Questions[i].ANSWERS[3]+"<BR>";
		_msg+="<BR>";
	} else if (all_Questions[i].TYPE=="FB")
	{
	    if(!_FIRST_FB)
		{
		  _FIRST_FB=true;
		  _msg+="<h2>Fill in the Blank Questions</h2><BR>";
		}
	    _msg+="<h4>"+(i+1)+".<font color=red>"+all_Questions[i].QUESTION+"</font></h4>";
        _msg+="<input type=text name='q"+(1+i)+"'><br>";
		_msg+="<BR>"; 
	} else if (all_Questions[i].TYPE=="TF")
	{
	    if(!_FIRST_TF)
		{
		  _FIRST_TF=true;
		  _msg+="<h2>True/False Questions</h2><BR>";
		}
	    _msg+="<h4>"+(i+1)+".<font color=red>"+all_Questions[i].QUESTION+"</font></h4>";
		_msg+="<input type=radio name='q"+(1+i)+"' value='"+all_Questions[i].ANSWERS[0]+"'>"+all_Questions[i].ANSWERS[0]+"";
        _msg+="<BR>";
		_msg+="<input type=radio name='q"+(1+i)+"' value='"+all_Questions[i].ANSWERS[1]+"'>"+all_Questions[i].ANSWERS[1]+"";
        _msg+="<BR>";
	} else 
	{
	  _msg+="<H4>ERROR</H4><BR>";
	}
	document.write(_msg);
  }
}

/*
 * Multiple Choice Questions Follow
 */
add_Q("MC",0,"What handles an &quot;event&quot; in JavaScript?",1,"A command called event handler.","A command called porter.","Javascript event activist.","Javascript cannot handle events.")
add_Q("MC",0,"Objects, properties and methods are separated by ...",2,"Forward slashes","Commas","Dots","Curly brackets");
add_Q("MC",0,"What is the main difference between validating and verifying an e-mail address by using Javascript?",4,"There is no difference.","The main difference is that the validation script sometimes can fail. Verification scripts can never fail.","Verification is performed faster than validation.","Validation checks if the user entered a proper form for an e-mail address, but actually does not verify that the address really exists.");
add_Q("MC",0,"To write Javascript you need...",3,"...a program called ColdFusion.","...an expensive compiler program.","...a simple text editor.","...a program called New Wave.");
add_Q("MC",0,"What is a cookie?",1,"A unique nugget of information that a Web server gives to your browser when the two first meet.","Sweet stuff you keep in a cookie jar.","A computer virus.","An applet also known as a worm.");
add_Q("MC",0,"What technique do you use in a slide show or a cycling banner to make sure the user has seen all of the graphic before displaying the next?",4,"You need to create an array.","Use a for loop.","The recycle() function.","The rotate() function.");
add_Q("MC",0,"Which one of the following can be done by using Javascript?",4,"Displaying time and day.","Scrolling status bar.","Rollovers.","All of the above.");
add_Q("MC",0,"What is DHTML?",4,"A collection of the following programming languages: C+, Visual Basic, Java and Javascript.","A combination of HTML, CSS and Javascript.","Refers to a combination of all computer programming languages.","Data Interaction Transcript Modifier Language.");
add_Q("MC",0,"How can you debug Javascript?",4,"Only with the help of madou Wane.","You must pay a fee to a computer repair technician to debug Javascript."," You can use a full-featured debugger program available from companies such as Netscape and Microsoft.","Javascript cannot be debugged.");
add_Q("MC",0,"Which of the following statement is true?",1,"Javascript can interact with a Java applet.","Javascript cannot interact with a Java applet. If you try, your browser will crash.","Javascript and Java applet are the same.","Javascript will only work if you run Java applets on the same page.");

/*
 * Fill-In-The-Blank Questions Follow
 */
add_Q("FB",0,"Who invented javascript?",1,"netscape");
add_Q("FB",0,"Javacript is an example of what type of language?",1,"interpreted");
add_Q("FB",0,"Is Javascript weakly or strongly typed?",1,"weakly");
add_Q("FB",0,"What is the process called to append literals?",1,"concatenation");
add_Q("FB",0,"Color is expressed in what type of notation?",1,"hexadecimal");

/*
 * True-False Questions Follow
 */
add_Q("TF",0,"The first element in an array is always 0.",1,"true","false");
add_Q("TF",0,"A function call evokes a function.",1,"true","false");
add_Q("TF",0,"DOM stands for Document Object Model.",1,"true","false");
add_Q("TF",0,"'Name.Number' will determine the number of elements in an array.",2,"true","false");
add_Q("TF",0,"JavaScript must never be placed in the head of the document.",2,"true","false");

/*
 * Extra Credit True-False Questions Follow
 */
add_Q("TF",1,"Mui Farata is from Chad.",1,"true","false");
add_Q("TF",1,"Fifty percent of the inhabitants of Chad are over 50 years of age.",2,"true","false");
add_Q("TF",1,"The official currency of Chad is the dirham.",2,"true","false");
add_Q("TF",1,"The people of Chad are the Bambara, Tuareg, Dogon, Songhai, Senoufou and Fulani.",1,"true","false");
add_Q("TF",1,"Chad is in West Africa.",1,"true","false");

var answers=new Array();

function validate() 
{
for(var i=0;i<all_Questions.length;i++)
{
  if(!all_Questions[i].EXTRA)
    if(all_Questions[i].TYPE=="MC" || all_Questions[i].TYPE=="TF")
	{
	  var _found=false;
	  for(var j=0;j<all_Questions[i].ANSWERS.length;j++)
	  {
	    if(eval("document.quiz.q"+(1+i)+"[j].checked"))
		  _found=true;
	  }
	  if(!_found) return false;
	} else { 	  /* getting lazy -- what else could it be.. */
	  if(!eval("document.quiz.q"+(1+i)+".value"))
	    return false;
	}
  }
 return true;
} 


function getScore() 
{ 
  if(!validate()) {alert("Tisk TisknYou did not answer all the manditory questions.");return;}
  
  var correctAnswers = "";
  var MUL_CHOICE=new Array("A","B","C","D");
  var TF_CHOICE=new Array("True","False");
  var temp="";
  var score = 0; 
  for(var i=0;i<all_Questions.length;i++)
  {
    if(all_Questions[i].TYPE=="MC" || all_Questions[i].TYPE=="TF")
	{
	  if(all_Questions[i].TYPE=="MC")
        temp=MUL_CHOICE[all_Questions[i].CORRECT_ANSWER];
	  else
	    temp=TF_CHOICE[all_Questions[i].CORRECT_ANSWER];
	  if(eval("document.quiz.q"+(1+i)+"[all_Questions[i].CORRECT_ANSWER].checked"))
	    score++;
	}
	else if (all_Questions[i].TYPE=="FB")
	{
	  temp=all_Questions[i].ANSWERS[all_Questions[i].CORRECT_ANSWER];
	  if(document.quiz.q11.value==all_Questions[i].ANSWERS[0])
	    score++;
	}
	else
	{
	  alert("Hey- how did you get here?");
	}
    correctAnswers += (1+i) + ". " + temp + "rn"; 
  }
  
  score = Math.round(score/(all_Questions.length-1)*100); 
  document.quiz.percentage.value = score + "%"; 
  document.quiz.solutions.value = correctAnswers; 
} 
</script> 

Body Code:

<form onsubmit="return false" name=quiz> 
<script>
show_Questions();
</script>
<BR>
<input type=button name=Button value="Get score" onClick="getScore()"><input type=reset value="Clear"> 
<BR>Score = <input type=text name=percentage value="" size=15> 
<BR>Correct answers: 
<BR><textarea name=solutions rows=25 cols=20 wrap=virtual></textarea> 
</form>

Times Viewed: 8

Leave a Reply

Your email address will not be published. Required fields are marked *