Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function solve(input){
- let userList = new Set();
- let articleList = new Set();
- let articleInstances = [];
- class Article{
- constructor(articleName){
- this.title = articleName;
- this.commentedUsers = [];
- this.comments = [];
- this.size = 0;
- }
- addComment(user,comment){
- this.commentedUsers.push(user);
- this.comments.push(comment);
- let currentLength = this.size;
- currentLength ++;
- this.size = currentLength;
- }
- }
- ///functions
- const addArticle = (name) =>{
- if(!Array.from(articleList).includes(name)){
- let article = new Article(name);
- articleInstances.push(article);
- articleList.add(name);
- }
- }
- const addInfo = (username, articleName, commentTitle,commentContent) => {
- let users = Array.from(userList);
- let articles = Array.from(articleList);
- let index = articles.indexOf(articleName);
- if(users.includes(username) && index > -1){
- // create comment object
- let comment = {};
- comment.title = commentTitle
- comment.content = commentContent;
- comment.name = username;
- //get article from article instances
- let articleObject = articleInstances[index];
- articleObject.addComment(username,comment)
- //update article
- }
- }
- //functions End
- for (const line of input) {
- let arr = line.split(' ');
- if(arr[0] === 'user'){
- let userName = arr[1];
- //addUser
- userList.add(userName);
- }else if(arr[0] === 'article'){
- //addArticle
- let articleName = arr.slice(1).join(' ');
- addArticle(articleName);
- }else if(arr[1] === 'posts' && arr[2] === 'on'){
- //add info
- let user = arr[0];
- let helper = arr.slice(3,4)[0];
- let article = helper.split(':')[0];
- let commentPart = line.split(': ')[1];
- let [commentTitle, commentContent] = commentPart.split(', ');
- addInfo(user, article, commentTitle,commentContent);
- }
- }
- let sortedInstances = articleInstances.slice(0).sort((a,b)=>{
- return b.size - a.size;
- })
- for (const article of sortedInstances) {
- console.log(`Comments on ${article.title}`);
- let commentaries = article.comments.sort((a,b)=>{
- return a.name.localeCompare(b.name);
- })
- commentaries.forEach(one=>{
- console.log(`--- From user ${one.name}: ${one.title} - ${one.content}`);
- })
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment