krasizorbov

Comments

Jul 7th, 2020
1,897
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solution(input = []) {
  2.   let data = new Map();
  3.   let users = [];
  4.   let articles = [];
  5.   for (let i = 0; i < input.length; i++) {
  6.     if (input[i].includes("user")) {
  7.       let userName = input[i].split(" ")[1];
  8.       if (!users.includes(userName)) {
  9.         users.push(userName);
  10.       }
  11.     } else if (input[i].includes("article")) {
  12.       let articleName = input[i].split(" ")[1];
  13.       if (!articles.includes(articleName)) {
  14.         articles.push(articleName);
  15.         data.set(articleName, []);
  16.       }
  17.     } else if (input[i].includes("posts")) {
  18.       let line = input[i].split(": ");
  19.       let userName = line[0].split(" ")[0];
  20.       let articleName = line[0].split(" ")[3];
  21.       let title = line[1].split(", ")[0];
  22.       let content = line[1].split(", ")[1];
  23.       if (users.includes(userName) && articles.includes(articleName)) {
  24.         let obj = { userName: userName, title: title, content: content };
  25.         data.get(articleName).push(obj);
  26.       }
  27.     }
  28.   }
  29.   data = new Map([...data.entries()].sort((a, b) => b[1].length - a[1].length));
  30.   for (const d of data) {
  31.     console.log(`Comments on ${d[0]}`);
  32.     d[1].sort((a, b) => a.userName.localeCompare(b.userName));
  33.     for (const obj of d[1]) {
  34.       console.log(
  35.         `--- From user ${obj.userName}: ${obj.title} - ${obj.content}`
  36.       );
  37.     }
  38.   }
  39. }
  40. solution([
  41.   "user aUser123",
  42.   "someUser posts on someArticle: NoTitle, stupidComment",
  43.   "article Books",
  44.   "article Movies",
  45.   "article Shopping",
  46.   "user someUser",
  47.   "user uSeR4",
  48.   "user lastUser",
  49.   "uSeR4 posts on Books: I like books, I do really like them",
  50.   "uSeR4 posts on Movies: I also like movies, I really do",
  51.   "someUser posts on Shopping: title, I go shopping every day",
  52.   "someUser posts on Movies: Like, I also like movies very much",
  53. ]);
Advertisement
Add Comment
Please, Sign In to add comment