Test Scores Tallied

[Total: 0    Average: 0/5]

This shows how to use constructs and double arrays to hold test scores for a given class.

Body Code:

<!--
  -- In a particular class there are 5 students and 4 tests are given.
  -- Use and initialize a doubly dimensioned array to hold these test scores.
  -- The output of the script is to be rows containing each students scores,
  -- followed by that students average.
  --
  -- Below each column of test scores is to be printed the average test score
  -- for that test , and (on that row, under the students average) the average
  -- score for all tests and all students. The script must calculate the
  -- averages from the data in the doubly dimenioned array. 
  -->

<script>
/*
 * Construct for the object TEST
 */
var a_test = function(_score)
{
  this.SCORE = _score;
  this.ScoreIs = function() { return this.SCORE; }
}

/*
 * Construct for the object STUDENT
 */
 var a_student = function(_name)
 {
   this.NAME = _name;
   this.NameIs = function(){ return this.NAME; }
   this.grades = new Array();
   this.TestsTaken = function() { return (this.grades.length?this.grades.length:0); }
   this.TestGrade = function(_grade)
                    {
					  /*
					   * TestsTaken is acurate - but the index for first test starts at 0
					   * So the next allocation in the array is TestsTaken
					   */
					  this.grades[this.TestsTaken()]=new a_test(_grade);
					}
   this.TestAvg = function()
                  {
				    var _total=0;
					var _tests=0;
					for(var index=0;index<this.grades.length;index++)
					{
					  _tests++;
					  _total+=this.grades[index].ScoreIs();
					}
					return (parseInt(_total/_tests));
				  }
   this.ShowTestGrades = function()
                         {
						   var _return="";
						   for(var index=0;index<this.grades.length;index++)
						   {
						     _return+=this.grades[index].ScoreIs();
							 if(index!=this.grades.length-1)
							 {
							   _return+="</td><td>";
							 }
						   }
						   return(_return);
						 }
  this.TestGradesAre = function()
                       {
					     var _return="";
						 for(var index=0;index<this.grades.length;index++)
						 {
						   _return+=this.grades[index].ScoreIs();
						   if(index!=this.grades.length-1)
						   {
						     _return+="|";
						   }
						 }
					     return(_return);
					   }
 }

/*
 * Construct for the object CLASS
 */
var a_class = function(_name)
{
  this.NAME = _name;
  this.NameIs = function() { return this.NAME;}
  this.students = new Array();
  this.AddStudent = function(_name)
                    {
					  this.students[this.students.length?this.students.length:0]=new a_student(_name);
					}
  this.TestResult = function(_name,_grade)
                    {
					  for (var index=0; index < this.students.length; index++)
					  {
					    if(this.students[index].NameIs() == _name)
						{
						  this.students[index].TestGrade(_grade);
						}
					  }
					}
  this.ShowResults = function()
                     {
					   var _return="Class: "+this.NAME+"<BR><BR><table border=1 cellpadding=5 cellspacing=3>";
					   /*
					    * _scores will be a double array to hold scores.
						*/
					   var _scores = new Array();
					   for (var index=0; index < this.students.length; index++)
					   {
					      /*
						   * for TOTAL math later
						   */
						 _scores[index]=this.students[index].TestGradesAre().split("|");

					     _return+="<tr><td>";
					     _return+=this.students[index].NameIs();
						 _return+="</td><td>";
						 _return+=this.students[index].ShowTestGrades();
						 _return+="</td><td><b>";
						 _return+=this.students[index].TestAvg();
						 _return+="</b></td></tr>";
					   }
					   /*
					    * Totals:
						*/
					   _return+="<tr><td>AVG--&gt;</td>";
					   var _avg_total=0;
					   for(var index_TestGrade=0; index_TestGrade<_scores[0].length; index_TestGrade++)
					   {
					     var _score=0;
						 var _total=0;
					     for(var index_Student=0; index_Student<_scores.length; index_Student++)
						 {
						   _score+=parseInt(_scores[index_Student][index_TestGrade]);
						   _total++;
						 }
						 _return+="<td><b>";
						 _return+=parseInt(_score/_total);
						 _avg_total+=parseInt(_score/_total);
						 _return+="</b></td>";
					   }
					   _return+="<td><b>"+parseInt(_avg_total/_scores.length)+"</b></td>";
					   _return+="</tr>";


					   _return+="</table>";
					   return(_return);
					 }
}

var myClass = new a_class("History");
    myClass.AddStudent("Tom");
	myClass.AddStudent("Bob");
	myClass.AddStudent("Joe");
	myClass.AddStudent("Boe");
	myClass.AddStudent("Jon");
	
	myClass.TestResult("Tom",95);
	myClass.TestResult("Bob",90);
	myClass.TestResult("Joe",85);
	myClass.TestResult("Boe",80);
	myClass.TestResult("Jon",75);

	myClass.TestResult("Tom",90);
	myClass.TestResult("Bob",85);
	myClass.TestResult("Joe",80);
	myClass.TestResult("Boe",75);
	myClass.TestResult("Jon",70);

	myClass.TestResult("Tom",100);
	myClass.TestResult("Bob",95);
	myClass.TestResult("Joe",90);
	myClass.TestResult("Boe",85);
	myClass.TestResult("Jon",80);

	myClass.TestResult("Tom",80);
	myClass.TestResult("Bob",70);
	myClass.TestResult("Joe",60);
	myClass.TestResult("Boe",40);
	myClass.TestResult("Jon",55);

	myClass.TestResult("Tom",15);
	myClass.TestResult("Bob",20);
	myClass.TestResult("Joe",35);
	myClass.TestResult("Boe",40);
	myClass.TestResult("Jon",60);

    document.write(myClass.ShowResults());
</script>

Times Viewed: 4

Leave a Reply

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