Advertisement
Guest User

Untitled

a guest
Jun 23rd, 2021
853
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class Story {
  2.     constructor(title, creator) {
  3.         this.title = title;
  4.         this.creator = creator;
  5.         this._comments = [];
  6.         this._likes = [];
  7.     }
  8.  
  9.     get likes() {
  10.         if (this._likes.length === 0) {
  11.             return `${this.title} has 0 likes`;
  12.         } else if (this._likes.length === 1) {
  13.             return `${this._likes[0]} likes this story!`;
  14.         }
  15.  
  16.         return `${this._likes[0]} and ${this._likes.slice(1).length} others like this story!`;
  17.     }
  18.  
  19.     like(username) {
  20.         if (this._likes.includes(username)) {
  21.             throw new Error("You can't like the same story twice!");
  22.         }
  23.  
  24.         if (username === this.creator) {
  25.             throw new Error("You can't like your own story!");
  26.         }
  27.  
  28.         this._likes.push(username);
  29.         return `${username} liked ${this.title}!`;
  30.     }
  31.  
  32.     dislike(username) {
  33.         if (!this._likes.includes(username)) {
  34.             throw new Error("You can't dislike this story!");
  35.         }
  36.  
  37.         this._likes = this._likes.filter(user => user !== username);
  38.  
  39.         return `${username} disliked ${this.title}`;
  40.     }
  41.  
  42.     comment(username, content, id) {
  43.         let comment = {
  44.             Id: id,
  45.             Username: username,
  46.             Content: content,
  47.             Replies: [],
  48.         };
  49.  
  50.         let commentWithId = this._comments.find(c => c.Id === id);
  51.  
  52.         if (commentWithId) {
  53.             let replyID = Number(commentWithId.Id + '.' + (commentWithId.Replies.length + 1));
  54.             let reply = {
  55.                 Id: replyID,
  56.                 Username: username,
  57.                 Content: content,
  58.             };
  59.  
  60.             commentWithId.Replies.push(reply);
  61.  
  62.             return 'You replied successfully';
  63.         }
  64.  
  65.         comment.Id = this._comments.length + 1;
  66.         this._comments.push(comment);
  67.  
  68.         return `${username} commented on ${this.title}`;
  69.     }
  70.  
  71.     toString(sortingType) {
  72.         let result = [];
  73.         result.push(`Title: ${this.title}`);
  74.         result.push(`Creator: ${this.creator}`);
  75.         result.push(`Likes: ${this._likes.length}`);
  76.         result.push('Comments:');
  77.  
  78.         if (this._comments.length > 0) {
  79.             let sortedComments = this._sortCriteria(this._comments, sortingType);
  80.             result.push(this._sortThem(sortedComments, sortingType));
  81.         }
  82.       return result.join('\n');
  83.     }
  84.  
  85.     _sortCriteria(commentsOrReplies, criteria) {
  86.         let copyCommentsOrReplies = commentsOrReplies.slice();
  87.         let sortedCommentsOrReplies = null;
  88.  
  89.         if (criteria === 'asc') {
  90.             sortedCommentsOrReplies = copyCommentsOrReplies.sort((a, b) => a.Id - b.Id);
  91.         } else if (criteria === 'desc') {
  92.             sortedCommentsOrReplies = copyCommentsOrReplies.sort((a, b) => b.Id - a.Id);
  93.         } else if (criteria === 'username') {
  94.             sortedCommentsOrReplies = copyCommentsOrReplies.sort((a, b) => a.Username.localeCompare(b.Username));
  95.         }
  96.  
  97.  
  98.         return sortedCommentsOrReplies;
  99.     }
  100.  
  101.     _sortThem(sortedComments, criteria) {
  102.         let commentAndReplies = [];
  103.  
  104.         for (let comment of sortedComments) {
  105.             commentAndReplies.push(`-- ${comment.Id}. ${comment.Username}: ${comment.Content}`);
  106.  
  107.             if (comment.Replies.length > 0) {
  108.                 let sortedReplies = this._sortCriteria(comment.Replies, criteria);
  109.                 sortedReplies.forEach(r => commentAndReplies.push(`--- ${r.Id}. ${r.Username}: ${r.Content}`));
  110.             }
  111.         }
  112.  
  113.         return commentAndReplies.join('\n');
  114.     }
  115. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement