Advertisement
dimipan80

Exams - Students, Courses, Grades, Visits

Nov 20th, 2014
201
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /* You are given a list of students score given as text table with the following columns:
  2.  student name, course, grade, number of visits. A student can have several grades and visits
  3.  for the same course. Write a JavaScript function to aggregate the results and print then as JSON string.
  4.  The input is passed as array of strings holding the table lines. Print at the console a JSON string
  5.  that holds the courses (in alphabetical order), the average grade and average visits for each course
  6.  and a list of students for each course (in alphabetical order). Duplicates should be removed
  7.  (all strings are case-sensitive). Please follow exactly the JSON format from the example below.
  8.  The average numbers should be rounded to 2 digits after the decimal point and printed without
  9.  trailing zeroes. */
  10.  
  11. "use strict";
  12.  
  13. function aggregateStudentsResults(arr) {
  14.         var dataMap = {};
  15.  
  16.     for (var i = 0; i < args.length; i += 1) {
  17.         var studentData, student, course, grade, visits;
  18.         studentData = args[i].split(/\|/).filter(Boolean);
  19.         student = studentData[0].trim();
  20.         course = studentData[1].trim();
  21.         grade = parseFloat(studentData[2]);
  22.         visits = parseFloat(studentData[3]);
  23.  
  24.         if (!(dataMap.hasOwnProperty(course))) {
  25.             dataMap[course] = { 'avgGrade': [], 'avgVisits': [], 'students': [] };
  26.         }
  27.  
  28.         dataMap[course].avgGrade.push(grade);
  29.         dataMap[course].avgVisits.push(visits);
  30.         if (dataMap[course].students.indexOf(student) < 0) {
  31.             dataMap[course].students.push(student);
  32.         }
  33.     }
  34.  
  35.     function getAverageNumber(arr) {
  36.         var sum = 0;
  37.             arr.forEach(function (num) {
  38.             sum += num;
  39.             });
  40.  
  41.         return Number((sum / arr.length).toFixed(2));
  42.     }
  43.  
  44.     function sortingObject(object) {
  45.         var sortedKeys = Object.keys(object).sort();
  46.         var newOject = {};
  47.         for (var i = 0; i < sortedKeys.length; i += 1) {
  48.             if (object.hasOwnProperty(sortedKeys[i])) {
  49.                 newOject[sortedKeys[i]] = {};
  50.                 newOject[sortedKeys[i]].avgGrade = getAverageNumber(object[sortedKeys[i]].avgGrade);
  51.                 newOject[sortedKeys[i]].avgVisits = getAverageNumber(object[sortedKeys[i]].avgVisits);
  52.                 newOject[sortedKeys[i]].students = object[sortedKeys[i]].students.sort();
  53.             }
  54.         }
  55.  
  56.         return newOject;
  57.     }
  58.  
  59.     dataMap = sortingObject(dataMap);
  60.     console.log(JSON.stringify(dataMap));
  61. }
  62.  
  63. aggregateStudentsResults([
  64.     'Peter Nikolov | PHP  | 5.50 | 8',
  65.     'Maria Ivanova | Java | 5.83 | 7',
  66.     'Ivan Petrov   | PHP  | 3.00 | 2',
  67.     'Ivan Petrov   | C#   | 3.00 | 2',
  68.     'Peter Nikolov | C#   | 5.50 | 8',
  69.     'Maria Ivanova | C#   | 5.83 | 7',
  70.     'Ivan Petrov   | C#   | 4.12 | 5',
  71.     'Ivan Petrov   | PHP  | 3.10 | 2',
  72.     'Peter Nikolov | Java | 6.00 | 9'
  73. ]);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement