maymunskoa

03. Comments (Assoc Array)

Mar 9th, 2020
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve(input){
  2.  
  3.     let userList = new Set();
  4.     let articleList = new Set();
  5.     let articleInstances = [];
  6.  
  7. class Article{
  8.     constructor(articleName){
  9.         this.title = articleName;
  10.         this.commentedUsers = [];
  11.         this.comments = [];
  12.         this.size = 0;
  13.     }
  14.  
  15.     addComment(user,comment){
  16.         this.commentedUsers.push(user);
  17.         this.comments.push(comment);
  18.         let currentLength = this.size;
  19.         currentLength ++;
  20.         this.size = currentLength;
  21.     }
  22.  
  23.    
  24. }
  25.    
  26.  
  27. ///functions
  28. const addArticle = (name) =>{
  29.  
  30.     if(!Array.from(articleList).includes(name)){
  31.         let article = new Article(name);
  32.         articleInstances.push(article);
  33.         articleList.add(name);
  34.     }
  35. }
  36. const addInfo = (username, articleName, commentTitle,commentContent) => {
  37.     let users = Array.from(userList);
  38.     let articles = Array.from(articleList);
  39.     let index = articles.indexOf(articleName);
  40.     if(users.includes(username) && index > -1){
  41.         // create comment object
  42.         let comment = {};
  43.         comment.title = commentTitle
  44.         comment.content = commentContent;
  45.         comment.name = username;
  46.         //get article from article instances
  47.         let articleObject = articleInstances[index];
  48.         articleObject.addComment(username,comment)
  49.         //update article
  50.     }
  51. }
  52. //functions End
  53.  
  54.  
  55. for (const line of input) {
  56.     let arr = line.split(' ');
  57.  
  58.     if(arr[0] === 'user'){
  59.         let userName = arr[1];
  60.          //addUser
  61.             userList.add(userName);
  62.     }else if(arr[0] === 'article'){
  63.         //addArticle
  64.         let articleName = arr.slice(1).join(' ');
  65.         addArticle(articleName);
  66.     }else if(arr[1] === 'posts' && arr[2] === 'on'){
  67.         //add info
  68.         let user = arr[0];
  69.         let helper = arr.slice(3,4)[0];
  70.         let article = helper.split(':')[0];
  71.         let commentPart = line.split(': ')[1];
  72.         let [commentTitle, commentContent] = commentPart.split(', ');
  73.        
  74.        addInfo(user, article, commentTitle,commentContent);
  75.     }
  76. }
  77.  
  78.  
  79.  
  80. let sortedInstances = articleInstances.slice(0).sort((a,b)=>{
  81.     return b.size - a.size;
  82. })
  83.  
  84. for (const article of sortedInstances) {
  85.     console.log(`Comments on ${article.title}`);
  86.     let commentaries = article.comments.sort((a,b)=>{
  87.         return a.name.localeCompare(b.name);
  88.     })
  89.     commentaries.forEach(one=>{
  90.         console.log(`--- From user ${one.name}: ${one.title} - ${one.content}`);
  91.        
  92.     })
  93. }
  94.  
  95. }
Advertisement
Add Comment
Please, Sign In to add comment