SHARE
TWEET

Untitled

a guest Feb 23rd, 2020 81 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class Article {
  2.  
  3.     _comments = [];
  4.     _likes = [];
  5.  
  6.     constructor(title, creator) {
  7.         this.title = title,
  8.             this.creator = creator
  9.  
  10.     }
  11.  
  12.     get likes() {
  13.         if (this._likes.length === 0) {
  14.             return `${this.title} has 0 likes`
  15.         }
  16.  
  17.         if (this._likes.length === 1) {
  18.             return `${this._likes[0]} likes this article!`
  19.         }
  20.  
  21.         return `${this._likes[0]} and ${this._likes.length - 1} others like this article!`
  22.  
  23.     }
  24.  
  25.     like(username) {
  26.  
  27.         if (this._likes.includes(username)) {
  28.             throw new Error("You can't like the same article twice!")
  29.         }
  30.  
  31.         if (username === this.creator) {
  32.             throw new Error("You can't like your own articles!")
  33.         }
  34.  
  35.         this._likes.push(username)
  36.  
  37.         return `${username} liked ${this.title}!`
  38.     }
  39.  
  40.     dislike(username) {
  41.         if (this._likes[0] !== username) {
  42.             throw new Error("You can't dislike this article!")
  43.         }
  44.  
  45.         this._likes.shift()
  46.  
  47.         return `${username} disliked ${this.title}`
  48.     }
  49.  
  50.     comment(username, content, id) {
  51.         let comment = this._comments.find(o => o.id === id)
  52.  
  53.  
  54.         if (id === undefined || comment === undefined) {
  55.  
  56.             id = this._comments.length + 1
  57.  
  58.             comment = { id: id, username: username, content: content, replies: [] };
  59.             this._comments.push(comment)
  60.             return `${username} commented on ${this.title}`
  61.         }
  62.  
  63.         if (comment !== undefined) {
  64.             let replyId = `${id}.${comment.replies.length + 1}`
  65.             let reply = { id: replyId, username: username, content: content }
  66.             comment.replies.push(reply);
  67.  
  68.             return "You replied successfully"
  69.         }
  70.  
  71.  
  72.  
  73.     }
  74.  
  75.     toString(sortingType) {
  76.         if (sortingType === "asc") {
  77.             this._comments = this._comments.sort((a, b) => {
  78.                 return Number(a.id) - Number(b.id)
  79.             })
  80.  
  81.             this._comments.forEach(comment => {
  82.                 comment.replies.sort((a, b) => {
  83.                     return Number(a.id) - Number(b.id)
  84.                 })
  85.             })
  86.  
  87.         }
  88.  
  89.         if (sortingType === "desc") {
  90.             this._comments = this._comments.sort((a, b) => {
  91.                 return Number(b.id) - Number(a.id)
  92.             })
  93.  
  94.             this._comments.forEach(comment => {
  95.                 comment.replies.sort((a, b) => {
  96.                     return Number(b.id) - Number(a.id)
  97.                 })
  98.             })
  99.         }
  100.  
  101.         if (sortingType === "username") {
  102.             this._comments.sort((a, b) => {
  103.                 return a.username.localeCompare(b.username)
  104.             })
  105.  
  106.             this._comments.forEach(comment => {
  107.                 comment.replies.sort((a, b) => {
  108.                     return a.username.localeCompare(b.username)
  109.                 })
  110.             })
  111.         }
  112.  
  113.         let result = `Title: ${this.title}\n` + `Creator: ${this.creator}\n` + `Likes: ${this._likes.length}\n` + `Comments:\n`
  114.  
  115.         this._comments.forEach(comment => {
  116.             result += `-- ${comment.id}. ${comment.username}: ${comment.content}\n`;
  117.             comment.replies.forEach(reply => {
  118.                 result += `--- ${reply.id}. ${reply.username}: ${reply.content}\n`
  119.             })
  120.         })
  121.  
  122.         return result.trim()
  123.     }
  124.  
  125. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top