SHARE
TWEET

Comments

a guest Nov 22nd, 2019 88 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve(inputs) {
  2.     let users = [];
  3.     let articles = [];
  4.     let commentsByArticle = {};
  5.     for (let input of inputs) {
  6.         if (input.startsWith('user ')) {
  7.             let user = input.split(' ')[1];
  8.             users.push(user);
  9.         } else if (input.startsWith('article ')) {
  10.             let article = input.split(' ')[1];
  11.             articles.push(article);
  12.         } else if (/^(.+) posts on (.+): (.+), (.+)$/.test(input)) {
  13.             let [match, user, article, title, content] = input.match(/^(.+) posts on (.+): (.+), (.+)$/);
  14.             if (users.includes(user) && articles.includes(article)) {
  15.                 if (!commentsByArticle.hasOwnProperty(article)) {
  16.                     commentsByArticle[article] = [];
  17.                 }
  18.                 commentsByArticle[article].push({user, title, content});
  19.             }
  20.         }
  21.     }
  22.     let sortedCommentsKeys = Object.keys(commentsByArticle).sort((a, b) => commentsByArticle[b].length - commentsByArticle[a].length);
  23.     for (const article of sortedCommentsKeys) {
  24.         console.log(`Comments on ${article}`);
  25.         let comments = commentsByArticle[article];
  26.         comments.sort((a, b) => a.user.localeCompare(b.user));
  27.         for (let comment of comments) {
  28.             console.log(`--- From user ${comment.user}: ${comment.title} - ${comment.content}`);
  29.         }
  30.     }
  31. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top