# Untitled

a guest
Jan 18th, 2022
422
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. function solve(arr) {
2.     let listOfCourses = {};
3.
4.     for (let element of arr) {
5.         if (!element.includes(`[`)) {
6.             let [courseName, capacity] = element.split(`: `);
7.             if (!listOfCourses.hasOwnProperty(courseName)) {
8.                 listOfCourses[courseName] = {
9.                     capacity: Number(capacity),
10.                     students: [],  //here
11.                 }
12.             } else {
13.                 listOfCourses[courseName].capacity += Number(capacity);
14.             }
15.         } else if (element.includes(`with email`)) {
16.             element = element.split(`[`);
18.             element = element[0].split(`]`);
19.             let creditCount = Number(element.shift());
20.             let temp = element[0].split(` `);
21.             let email = temp[3];
22.             let courseName = element[0].split(` joins `)[1];
23.             if (listOfCourses.hasOwnProperty(courseName) && listOfCourses[courseName].capacity > 0) {
24.                 listOfCourses[courseName].students.push({ //here
26.                     creditCount: creditCount,
27.                     email: email,
28.                 })
29.                 listOfCourses[courseName].capacity--;
30.             }
31.         }
32.     }
33.     let sortedByCapacity = Object.entries(listOfCourses)
34.         .sort((a, b) => Object.keys(b[1].students).length - Object.keys(a[1].students).length);
35.     for (let element of sortedByCapacity) {
36.         console.log(`\${element[0]}: \${element[1].capacity} places left`);
37.         element[1].students.sort((a, b) => b.creditCount - a.creditCount).forEach(x => console.log(`--- \${x.creditCount}: \${x.userName}, \${x.email}`)); //here
38.     }
39. }