krustev_84

Untitled

Sep 12th, 2020
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.53 KB | None | 0 0
  1. function comments(input) {
  2.  
  3. let usersList = [];
  4. let articleList = [];
  5. let obj = {};
  6.  
  7. input.forEach(line => {
  8.  
  9. if (line.split(' ')[0] === 'user') {
  10.  
  11. let splitedLine = line.split(' ');
  12. let user = splitedLine[1];
  13. usersList.push(user);
  14.  
  15. } else if (line.split(' ')[0] === 'article') {
  16.  
  17. let splitedLine = line.split(' ');
  18. let article = splitedLine[1];
  19. articleList.push(article);
  20.  
  21. } else {
  22.  
  23. let [whoPost, whichArt] = line.split(': ');
  24. let splitedWho = whoPost.split(' ');
  25. let user = splitedWho[0];
  26. let article = splitedWho[3];
  27. let splitedWhich = whichArt.split(', ');
  28. let comment = [];
  29. splitedWhich.forEach(comm => {
  30.  
  31. comment.push(comm);
  32. });
  33.  
  34. if (usersList.includes(user) && articleList.includes(article)) {
  35.  
  36. if (!obj[article]) {
  37. obj[article] = {};
  38. obj[article][user] = comment;
  39. } else {
  40. obj[article][user] = comment;
  41.  
  42. }
  43. }
  44. }
  45. });
  46.  
  47. let sortedArt = Object.entries(obj).sort((a, b) => {
  48. let aEntr = Object.entries(a[1]);
  49. let bEntr = Object.entries(b[1]);
  50.  
  51. return bEntr.length - aEntr.length;
  52. });
  53.  
  54. sortedArt.forEach(line => {
  55.  
  56. console.log(`Comments on ${line[0]}`);
  57.  
  58. let sortedUsers = Object.entries(line[1]).sort((a, b) => {
  59.  
  60. return a[0].localeCompare(b[0]);
  61. });
  62.  
  63. sortedUsers.forEach(user => {
  64.  
  65. console.log(`--- From user ${user[0]}: ${user[1].join(' - ')}`);
  66. });
  67.  
  68.  
  69. });
  70.  
  71.  
  72. }
Advertisement
Add Comment
Please, Sign In to add comment