Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* Write a program that reads a list of student grade entries and prints the average grade of every subject
- for each student. For example, we are given the following entries:
- Ivan Ivanov history 5
- Ivan Ivanov math 3
- Ivan Ivanov math 4
- Peter Petrov physics 2
- There are two students – Ivan Ivanov and Peter Petrov. Ivan has an average grade in history of 5.00 and
- in math of 3.50. Peter has an average grade in physics of 2.00. We print the students,
- sorted alphabetically by fullname. The subjects of each student should also be sorted alphabetically.
- The result is:
- Ivan Ivanov: [history – 5.00, math – 3.50]
- Peter Petrov: [physics – 2.00]
- At the first line a number n stays which says how many lines will follow.
- Each of the next n lines holds information in format:
- <First name> <Last name> <subject> <score>
- Print on the console in a row for each student in the following format:
- <First name> <Last name>: [<subject> - <average score>, <subject> - <average score>, …]
- The subjects of each student should be printed in alphabetical order.
- Students should be printed in alphabetical order of their full name.
- The average grade should be rounded to the second decimal. */
- "use strict";
- function studentAverageGrade(args) {
- var countRows = parseInt(args[0]);
- var studentList = {};
- for (var i = 1; i <= countRows; i += 1) {
- var line = args[i].split(/\s/);
- var name = line[0] + ' ' + line[1];
- var subject = line[2];
- var grade = parseFloat(line[3]);
- if (!(name in studentList)) {
- studentList[name] = {};
- }
- if (!(subject in studentList[name])) {
- studentList[name][subject] = [];
- }
- studentList[name][subject].push(grade);
- }
- studentList = sortStudentList(studentList);
- printResult(studentList);
- function sortStudentList(object) {
- var newObject = {};
- var sortedNames = Object.keys(object).sort();
- for (var i = 0; i < sortedNames.length; i += 1) {
- if (object.hasOwnProperty(sortedNames[i])) {
- newObject[sortedNames[i]] = {};
- var sortedSubjects = Object.keys(object[sortedNames[i]]).sort();
- for (var j = 0; j < sortedSubjects.length; j += 1) {
- if (object[sortedNames[i]].hasOwnProperty(sortedSubjects[j])) {
- newObject[sortedNames[i]][sortedSubjects[j]] =
- getSubjectAverageGrade(object[sortedNames[i]][sortedSubjects[j]]);
- }
- }
- }
- }
- return newObject;
- }
- function getSubjectAverageGrade(gradesArr) {
- if (gradesArr.length) {
- var average = 0;
- for (var grade in gradesArr) {
- if (gradesArr.hasOwnProperty(grade)) {
- average += gradesArr[grade];
- }
- }
- return (average / gradesArr.length).toFixed(2);
- }
- }
- function printResult(object) {
- var resultStr = '';
- for (var student in object) {
- if (object.hasOwnProperty(student)) {
- resultStr += student + ': [';
- var keys = Object.keys(object[student]);
- for (i = 0; i < keys.length; i += 1) {
- if (object[student].hasOwnProperty(keys[i])) {
- resultStr += keys[i] + ' - ' + object[student][keys[i]];
- if (i < keys.length - 1) {
- resultStr += ', '
- }
- }
- }
- resultStr += ']\n';
- }
- }
- console.log(resultStr);
- }
- }
- studentAverageGrade([
- '4',
- 'Ivan Ivanov history 5',
- 'Ivan Ivanov math 3',
- 'Ivan Ivanov math 4',
- 'Peter Petrov physics 2'
- ]);
- studentAverageGrade([
- '1',
- 'Ivan Ivanov history 5'
- ]);
- studentAverageGrade([
- '10',
- 'Todor Todorov history 5',
- 'Angel Angelov math 4',
- 'Ivan Ivanov history 4',
- 'Angel Angelov algebra 4',
- 'Angel Angelov history 4',
- 'Angel Angelov history 5',
- 'Ivan Ivanov algebra 6',
- 'Todor Todorov history 5',
- 'Ivan Ivanov algebra 5',
- 'Todor Todorov algebra 3'
- ]);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement