Todorov_Stanimir

03. Comments

Jul 6th, 2019
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function comments(input) {
  2.     let usersList = [];
  3.     let articlesList = [];
  4.     let output = {}
  5.  
  6.     for (let command of input) {
  7.         if (command.substr(0, 4) === 'user') {
  8.             let newUser = command.split(' ')[1];
  9.             usersList.push(newUser);
  10.         } else if (command.substr(0, 7) === 'article') {
  11.             let newArticle = command.split(' ')[1];
  12.             articlesList.push(newArticle);
  13.         } else {
  14.             let [userAndArticleName, commentTitleAndContent] = command.split(': ');
  15.             let [userName, articleName] = userAndArticleName.split(' posts on ');
  16.             let [commentTitle, commentContent] = commentTitleAndContent.split(', ');
  17.             if (usersList.includes(userName) && articlesList.includes(articleName)) {
  18.                 if (!Object.keys(output).includes(articleName)) {
  19.                     output[articleName] = [];
  20.                 }
  21.                 output[articleName].push([commentTitle, userName, commentContent])
  22.             }
  23.         }
  24.     }
  25.     output = Object.entries(output);
  26.     output.sort((a, b) => b[1].length / 2 - a[1].length / 2);
  27.     output.forEach(element => {
  28.         element[1].sort((a, b) => a[1].localeCompare(b[1]));
  29.  
  30.     });
  31.     output.forEach(element => {
  32.         console.log(`Comments on ${element[0]}`);
  33.         element[1].forEach(element => {
  34.             console.log(`--- From user ${element[1]}: ${element[0]} - ${element[2]}`);
  35.         });
  36.     });
  37. }
Advertisement
Add Comment
Please, Sign In to add comment