Advertisement
bebo231312312321

Untitled

Mar 17th, 2023
42
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function comments(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.                 if (!comments.has(article)) {
  26.                     comments.set(article, []);
  27.                 }
  28.                 comments.get(article).push(contentString);
  29.             }
  30.         }
  31.     }
  32.     let sortedComments = [...comments.entries()]
  33.     .sort((a, b) => b[1].length - a[1].length);
  34.  
  35.     for(let e of sortedComments){
  36.         let article = e[0];
  37.         let comments = e[1].sort((a, b) => b.substring(15).localeCompare(a.substring(15)));
  38.         console.log(`Comments on ${article}`);
  39.         for(let el of comments){
  40.             console.log(`${el}`);
  41.         }  
  42.     }
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement