Advertisement
_Lyuba_Ivanova_

Untitled

Oct 11th, 2019
713
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.94 KB | None | 0 0
  1. function solve(input) {
  2. let users = [];
  3. let articles = [];
  4. for (let i = 0; i < input.length; i++) {
  5. if (input[i].includes('article')) {
  6. let article = input[i].split(' ')[1];
  7. articles.push(article);
  8. delete input[i];
  9. } else if (input[i].includes('user')) {
  10. let user = input[i].split(' ')[1];
  11. users.push(user);
  12. delete input[i];
  13. }
  14. }
  15. let comments = new Map();
  16. for (let e of input) {
  17. if (e !== undefined) {
  18. let tokens = e.split(': ');
  19. let user = tokens[0].split(' ')[0];
  20. let article = tokens[0].split(' ')[3];
  21. let commentContent = tokens[1].split(', ').join(' - ');
  22.  
  23. if (users.includes(user) && articles.includes(article)) {
  24. let contentString = '--- From user ' + (user) + ': ' + (commentContent);
  25.  
  26. if (!comments.has(article)) {
  27. comments.set(article, []);
  28. }
  29. comments.get(article).push(contentString);
  30. }
  31. }
  32. }
  33.  
  34. let sortedComments = [...comments.entries()]
  35. .sort((a, b) => b[1].length - a[1].length);
  36.  
  37. for(let e of sortedComments){
  38. let article = e[0];
  39. let comments = e[1].sort((a, b) => a.localeCompare(b));
  40. console.log(`Comments on ${article}`);
  41. for(let el of comments){
  42. console.log(`${el}`);
  43. }
  44. }
  45. }
  46.  
  47. solve(['user aUser123',
  48. 'someUser posts on someArticle: NoTitle, stupidComment',
  49. 'article Books',
  50. 'article Movies',
  51. 'article Shopping',
  52. 'user someUser',
  53. 'user uSeR4',
  54. 'user lastUser',
  55. 'uSeR4 posts on Books: I like books, I do really like them',
  56. 'uSeR4 posts on Movies: I also like movies, I really do',
  57. 'someUser posts on Shopping: title, I go shopping every day',
  58. 'someUser posts on Movies: Like, I also like movies very much'
  59. ]);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement