Advertisement
Guest User

Untitled

a guest
Apr 6th, 2020
221
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class Article {
  2.     constructor(title, creator) {
  3.         this.title = title;
  4.         this.creator = creator;
  5.         this._comments = [];
  6.         this._likes = [];
  7.         this.id = 1;
  8.         this.first = '';
  9.     }
  10.    
  11.     get likes() {
  12.         if (this._likes.length === 0) {
  13.             return `${this.title} has 0 likes`;
  14.         }
  15.         if (this._likes.length === 1) {
  16.             return `${this._likes[0]} likes this article!`;
  17.         }
  18.         return `${this.first} and ${this._likes.length - 1} others like this article!`;
  19.     }
  20.     like(username) {
  21.  
  22.         if (this._likes.includes(username)) {
  23.             throw new Error('You can\'t like the same article twice!');
  24.         }
  25.         if (this.creator === username) {
  26.             throw new Error('You can\'t like your own articles!');
  27.         }
  28.         if (this._likes.length === 0) {
  29.             this.first = username;
  30.         }
  31.         this._likes.push(username);
  32.         return `${username} liked ${this.title}!`;
  33.     }
  34.     dislike(username) {
  35.         if (!this._likes.includes(username)) {
  36.             throw new Error('You can\'t dislike this article!');
  37.         }
  38.         let index = this._likes.findIndex(x => x === username);
  39.         this._likes.splice(index, 1);
  40.         return `${username} disliked ${this.title}`;
  41.     }
  42.     comment(username, content, id) {
  43.         let commentExists = this._comments.find(x => x.id === id);
  44.         if (!commentExists) {
  45.             let obj = {
  46.                 id: this.id++,
  47.                 username: username,
  48.                 content: content,
  49.                 replies: []
  50.             }
  51.             this._comments.push(obj);
  52.             return `${username} commented on ${this.title}`;
  53.         }
  54.         let replyObj = {
  55.             id: `${id}.${commentExists.replies.length + 1}`,
  56.             username: username,
  57.             content: content
  58.         }
  59.         commentExists.replies.push(replyObj);
  60.         return 'You replied successfully';
  61.     }
  62.     toString(sortingType) {
  63.         if (sortingType === 'asc') {
  64.             this._comments = this._comments.sort((a, b) => Number(a.id) - Number(b.id));
  65.         }
  66.         if (sortingType === 'desc') {
  67.             this._comments = this._comments.sort((a, b) => Number(b.id) - Number(a.id));
  68.         }
  69.         if (sortingType === 'username') {
  70.             this._comments = this._comments.sort((a, b) => a.username.localeCompare(b.username));
  71.         }
  72.         let result = '';
  73.         result += `Title: ${this.title}\n`;
  74.         result += `Creator: ${this.creator}\n`;
  75.         result += `Likes: ${this._likes.length}\n`;
  76.         result += `Comments:\n`;
  77.  
  78.         this._comments.forEach(x => {
  79.             result += `-- ${x.id}. ${x.username}: ${x.content}\n`;
  80.             let repliesComment = x.replies;
  81.             if (sortingType === 'asc') {
  82.                 repliesComment = repliesComment.sort((a, b) => Number(a.id) - Number(b.id));
  83.             }
  84.             if (sortingType === 'desc') {
  85.                 repliesComment = repliesComment.sort((a, b) => Number(b.id) - Number(a.id));
  86.             }
  87.             if (sortingType === 'username') {
  88.                 repliesComment = repliesComment.sort((a, b) => a.username.localeCompare(b.username));
  89.             }
  90.             repliesComment.forEach(m => {
  91.                 result += `--- ${m.id}. ${m.username}: ${m.content}\n`;
  92.             })
  93.         })
  94.         return result.trim();
  95.     }
  96. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement