Advertisement
PowerCell46

SoftUni Students JS

Mar 20th, 2023
734
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function softUniStudents(array) {
  2.  
  3.     let objOfCourses = {}
  4.     let objOfCoursesWithCapacities = {}
  5.  
  6.     for (let index in array) {
  7.         let currentInput = array[index].split(" ");
  8.         if (currentInput[0][currentInput[0].length - 1] === ":") {
  9.             let currentCourse = currentInput[0].split("");
  10.             currentCourse.pop();
  11.             currentCourse = currentCourse.join("");
  12.             let currentCourseCapacity = Number(currentInput[1]);
  13.             if (!objOfCourses.hasOwnProperty(currentCourse)) {
  14.                 objOfCourses[currentCourse] = {}
  15.                 objOfCoursesWithCapacities[currentCourse] = currentCourseCapacity;
  16.             } else {
  17.                 objOfCoursesWithCapacities[currentCourse] = (objOfCoursesWithCapacities[currentCourse] + currentCourseCapacity);
  18.             }
  19.         } else {
  20.             let currentUsername = currentInput[0].split("");
  21.             currentUsername.pop();
  22.             currentUsername = currentUsername.join("");
  23.             currentUsername = currentUsername.split("[");
  24.             let currentUsernameCredits = Number(currentUsername.pop());
  25.             currentUsername = currentUsername[0];
  26.             let currentEmail = currentInput[3];
  27.             let currentCourseName = currentInput[5];
  28.             let arrayOfCourses = Object.keys(objOfCourses);
  29.             if (arrayOfCourses.includes(currentCourseName) && objOfCoursesWithCapacities[currentCourseName] > 0) {
  30.                 objOfCoursesWithCapacities[currentCourseName] = objOfCoursesWithCapacities[currentCourseName] - 1;
  31.                 objOfCourses[currentCourseName][currentUsernameCredits] = {}
  32.                 objOfCourses[currentCourseName][currentUsernameCredits][currentUsername] = {}
  33.                 objOfCourses[currentCourseName][currentUsernameCredits][currentUsername] = currentEmail;
  34.             }
  35.         }
  36.     }
  37.     let objCourseToStudentsRatio = {}
  38.  
  39.     let arrayOfCourses = Object.keys(objOfCourses);
  40.  
  41.     for (let course of arrayOfCourses) {
  42.         let currentNumberOfStudents = Object.values(objOfCourses[course]).length;
  43.         objCourseToStudentsRatio[course] = currentNumberOfStudents;
  44.     }
  45.  
  46.     let arrayOfNumberOfStudentsOrdered = Object.values(objCourseToStudentsRatio).sort((a, b) => b - a);
  47.     for (let numberOfStudents of arrayOfNumberOfStudentsOrdered) {
  48.         for (let course of Object.keys(objCourseToStudentsRatio)) {
  49.             if (numberOfStudents === objCourseToStudentsRatio[course]) {
  50.                 console.log(`${course}: ${objOfCoursesWithCapacities[course]} places left`);
  51.                 delete objCourseToStudentsRatio[course];
  52.                 let arrayOfCreditsOfTheCurrentCourse = Object.keys(objOfCourses[course]);
  53.                 let arrayOfCreditsOfTheCurrentCourseOrdered = arrayOfCreditsOfTheCurrentCourse.map((x) => Number(x)).sort((a,b) => b - a);
  54.                 for (let currentCredits of arrayOfCreditsOfTheCurrentCourseOrdered) {
  55.                     let currentUsername = Object.keys(objOfCourses[course][currentCredits])[0];
  56.                     let currentEmail = Object.values(objOfCourses[course][currentCredits])[0];
  57.                     console.log(`--- ${currentCredits}: ${currentUsername}, ${currentEmail}`);
  58.                 }
  59.             }
  60.         }
  61.     }
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement