Advertisement
bebo231312312321

Untitled

Mar 17th, 2023
24
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.64 KB | None | 0 0
  1. function softUniStudents(array) {
  2.  
  3. let obj = {};
  4.  
  5. for(let el of array){
  6. if(el.includes(': ')){
  7. let [course, capacity] = el.split(': ');
  8. if(obj.hasOwnProperty(course) == false){
  9. obj[course] = {
  10. 'capacity': Number(capacity),
  11. 'count': 0,
  12. 'users': {}
  13. }
  14. continue;
  15. }
  16. obj[course]['capacity'] += Number(capacity);
  17. }
  18. if(el.includes('with')){
  19. let tokens = el.split(/[\[|\]]/g);
  20. let user = tokens.shift();
  21. let credit = tokens.shift();
  22. tokens = tokens[0].split(' ');
  23. let course = tokens.pop();
  24. let email = tokens[tokens.length - 2];
  25.  
  26. if(obj.hasOwnProperty(course) && obj[course]['capacity'] !== 0){
  27. if(obj[course]['capacity'])
  28. obj[course]['users'][Number(credit)] = {
  29. 'user': user,
  30. 'email': email};
  31. obj[course]['capacity']--;
  32. obj[course]['count']++;
  33. }
  34. }
  35. }
  36.  
  37. let sorted = Object.entries(obj).sort((a, b) => b[1].count - a[1].count);
  38. for(let [course, information] of sorted){
  39. console.log(`${course}: ${information.capacity} places left`);
  40. let sortedStudents = Object.entries(information['users'])
  41. .sort((a, b) => Number(b[0]) - Number(a[0]))
  42. .forEach(element => {
  43. console.log(`--- ${element[0]}: ${element[1]['user']}, ${element[1]['email']}`);
  44. });
  45.  
  46. }
  47.  
  48.  
  49.  
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement