Advertisement
dimipan80

Exams - School System (on JavaScript)

Jan 8th, 2015
233
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /* Write a program that reads a list of student grade entries and prints the average grade of every subject
  2.  for each student. For example, we are given the following entries:
  3.  Ivan Ivanov history 5
  4.  Ivan Ivanov math 3
  5.  Ivan Ivanov math 4
  6.  Peter Petrov physics 2
  7.  There are two students – Ivan Ivanov and Peter Petrov. Ivan has an average grade in history of 5.00 and
  8.  in math of 3.50. Peter has an average grade in physics of 2.00. We print the students,
  9.  sorted alphabetically by fullname. The subjects of each student should also be sorted alphabetically.
  10.  The result is:
  11.  Ivan Ivanov: [history – 5.00, math – 3.50]
  12.  Peter Petrov: [physics – 2.00]
  13.  At the first line a number n stays which says how many lines will follow.
  14.  Each of the next n lines holds information in format:
  15.  <First name> <Last name> <subject> <score>
  16.  Print on the console in a row for each student in the following format:
  17.  <First name> <Last name>: [<subject> - <average score>, <subject> - <average score>, …]
  18.  The subjects of each student should be printed in alphabetical order.
  19.  Students should be printed in alphabetical order of their full name.
  20.  The average grade should be rounded to the second decimal. */
  21.  
  22. "use strict";
  23.  
  24. function studentAverageGrade(args) {
  25.     var countRows = parseInt(args[0]);
  26.     var studentList = {};
  27.     for (var i = 1; i <= countRows; i += 1) {
  28.         var line = args[i].split(/\s/);
  29.         var name = line[0] + ' ' + line[1];
  30.         var subject = line[2];
  31.         var grade = parseFloat(line[3]);
  32.        
  33.         if (!(name in studentList)) {
  34.             studentList[name] = {};
  35.         }
  36.         if (!(subject in studentList[name])) {
  37.             studentList[name][subject] = [];
  38.         }
  39.         studentList[name][subject].push(grade);
  40.     }
  41.    
  42.     studentList = sortStudentList(studentList);
  43.     printResult(studentList);
  44.    
  45.     function sortStudentList(object) {
  46.         var newObject = {};
  47.         var sortedNames = Object.keys(object).sort();
  48.         for (var i = 0; i < sortedNames.length; i += 1) {
  49.             if (object.hasOwnProperty(sortedNames[i])) {
  50.                 newObject[sortedNames[i]] = {};
  51.                 var sortedSubjects = Object.keys(object[sortedNames[i]]).sort();
  52.                 for (var j = 0; j < sortedSubjects.length; j += 1) {
  53.                     if (object[sortedNames[i]].hasOwnProperty(sortedSubjects[j])) {
  54.                         newObject[sortedNames[i]][sortedSubjects[j]] =
  55.                             getSubjectAverageGrade(object[sortedNames[i]][sortedSubjects[j]]);
  56.                     }
  57.                 }
  58.             }
  59.         }
  60.        
  61.         return newObject;
  62.     }
  63.    
  64.     function getSubjectAverageGrade(gradesArr) {
  65.         if (gradesArr.length) {
  66.             var average = 0;
  67.             for (var grade in gradesArr) {
  68.                 if (gradesArr.hasOwnProperty(grade)) {
  69.                     average += gradesArr[grade];
  70.                 }
  71.             }
  72.            
  73.             return (average / gradesArr.length).toFixed(2);
  74.         }
  75.     }
  76.    
  77.     function printResult(object) {
  78.         var resultStr = '';
  79.         for (var student in object) {
  80.             if (object.hasOwnProperty(student)) {
  81.                 resultStr += student + ': [';
  82.                 var keys = Object.keys(object[student]);
  83.                 for (i = 0; i < keys.length; i += 1) {
  84.                     if (object[student].hasOwnProperty(keys[i])) {
  85.                         resultStr += keys[i] + ' - ' + object[student][keys[i]];
  86.                         if (i < keys.length - 1) {
  87.                             resultStr += ', '
  88.                         }
  89.                     }
  90.                 }
  91.                 resultStr += ']\n';
  92.             }
  93.         }
  94.         console.log(resultStr);
  95.     }
  96. }
  97.  
  98. studentAverageGrade([
  99.     '4',
  100.     'Ivan Ivanov history 5',
  101.     'Ivan Ivanov math 3',
  102.     'Ivan Ivanov math 4',
  103.     'Peter Petrov physics 2'
  104. ]);
  105.  
  106. studentAverageGrade([
  107.     '1',
  108.     'Ivan Ivanov history 5'
  109. ]);
  110.  
  111. studentAverageGrade([
  112.     '10',
  113.     'Todor Todorov history 5',
  114.     'Angel Angelov math 4',
  115.     'Ivan Ivanov history 4',
  116.     'Angel Angelov algebra 4',
  117.     'Angel Angelov history 4',
  118.     'Angel Angelov history 5',
  119.     'Ivan Ivanov algebra 6',
  120.     'Todor Todorov history 5',
  121.     'Ivan Ivanov algebra 5',
  122.     'Todor Todorov algebra 3'
  123. ]);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement