Ggodly

JS Advanced Exam - 20 February 2021 / 02. Story

Jun 23rd, 2021
860
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.  
  86.     _sortCriteria(commentsOrReplies, criteria) {
  87.         let copyCommentsOrReplies = commentsOrReplies.slice();
  88.         let sortedCommentsOrReplies = null;
  89.  
  90.         if (criteria === 'asc') {
  91.             sortedCommentsOrReplies = copyCommentsOrReplies.sort((a, b) => a.Id - b.Id);
  92.         } else if (criteria === 'desc') {
  93.             sortedCommentsOrReplies = copyCommentsOrReplies.sort((a, b) => b.Id - a.Id);
  94.         } else if (criteria === 'username') {
  95.             sortedCommentsOrReplies = copyCommentsOrReplies.sort((a, b) => a.Username.localeCompare(b.Username));
  96.         }
  97.  
  98.  
  99.         return sortedCommentsOrReplies;
  100.     }
  101.  
  102.     _sortThem(sortedComments, criteria) {
  103.         let commentAndReplies = [];
  104.  
  105.         for (let comment of sortedComments) {
  106.             commentAndReplies.push(`-- ${comment.Id}. ${comment.Username}: ${comment.Content}`);
  107.  
  108.             if (comment.Replies.length > 0) {
  109.                 let sortedReplies = this._sortCriteria(comment.Replies, criteria);
  110.                 sortedReplies.forEach(r => commentAndReplies.push(`--- ${r.Id}. ${r.Username}: ${r.Content}`));
  111.             }
  112.         }
  113.  
  114.         return commentAndReplies.join('\n');
  115.     }
  116. }
RAW Paste Data