Advertisement
Todorov_Stanimir

JS Advanced - 23 February 2020

Feb 23rd, 2020
219
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class Article {
  2.     _comments = [];
  3.     _likes = [];
  4.     constructor(title, creator) {
  5.         this.title = title;
  6.         this.creator = creator;
  7.     }
  8.  
  9.     get likes() {
  10.         if (this._likes.length === 0) {
  11.             return `${this.title} has 0 likes`;
  12.         }
  13.  
  14.         if (this._likes.length === 1) {
  15.             return `${this._likes[0]} likes this article!`;
  16.         }
  17.  
  18.         return `${this._likes[0]} and ${this._likes.length - 1} other like this article!`;
  19.     }
  20.  
  21.     like(username) {
  22.         if (this._likes.includes(username)) {
  23.             throw new Error(`You can\'t like the same artice twice!`);
  24.        }
  25.  
  26.        if (this.creator === username) {
  27.            throw new Error(`You can\'t like your own articles!`);
  28.        }
  29.  
  30.        this._likes.push(username);
  31.  
  32.        return `${username} liked ${this.title}`;
  33.    }
  34.  
  35.    dislike(username) {
  36.        const index = this._likes.findIndex((userLikes) => userLikes === username);
  37.  
  38.        if (index === -1) {
  39.            throw new Error(`You can\'t dislike this article!`);
  40.        }
  41.  
  42.        this._likes.splice(index, 1);
  43.  
  44.        return `${username} disliked ${this.title}`;
  45.    }
  46.  
  47.    comment(username, content, id) {
  48.        if (id === undefined || this._comments.findIndex(el => el.id === id) === -1) {
  49.  
  50.            const newComment = {
  51.                id: this._comments.length + 1,
  52.                username,
  53.                content,
  54.                replies: []
  55.            }
  56.  
  57.            this._comments.push(newComment);
  58.  
  59.            return `${username} commented on ${this.title}`;
  60.        }
  61.  
  62.        const indexComment = this._comments.findIndex(el => el.id === id);
  63.  
  64.        if (indexComment !== -1) {
  65.  
  66.            const newReply = {
  67.                id: `${indexComment + 1}.${this._comments[indexComment].replies.length + 1}`,
  68.                username,
  69.                content
  70.            }
  71.  
  72.            this._comments[indexComment].replies.push(newReply);
  73.  
  74.            return `You replied successfully`;
  75.        }
  76.    }
  77.  
  78.    toString(sortingCriteria) {
  79.  
  80.        let output = `Title: ${this.title}\nCreator: ${this.creator}\nLikes: ${this._likes.length}\n`;
  81.  
  82.        if (sortingCriteria === 'username') {
  83.  
  84.            this._comments.sort((a, b) => a.username.localeCompare(b.username));
  85.  
  86.            this._comments.forEach(comment => {
  87.                comment.replies.sort((a, b) => a.username.localeCompare(b.username));
  88.            });
  89.        }
  90.  
  91.        if (sortingCriteria === 'desc') {
  92.            this._comments.sort((a, b) => b.id - a.id);
  93.  
  94.            this._comments.forEach(comment => {
  95.                comment.replies.sort((a, b) => b.id - a.id);
  96.            });
  97.  
  98.        }
  99.  
  100.        this._comments.forEach(comment => {
  101.            output += `-- ${comment.id}. ${comment.username}: ${comment.content}\n`;
  102.            comment.replies.forEach(reply => {
  103.                output += `--- ${reply.id}. ${reply.username}: ${reply.content}\n`;
  104.            })
  105.        })
  106.  
  107.        return output.trim();
  108.  
  109.    }
  110. }
  111.  
  112.  
  113. let art = new Article("My Article", "Anny");
  114. art.like("John");
  115. console.log(art.likes);
  116. art.dislike("John");
  117. console.log(art.likes);
  118. art.comment("Sammy", "Some Content");
  119. console.log(art.comment("Ammy", "New Content"));
  120. art.comment("Zane", "Reply", 1);
  121. art.comment("Jessy", "Nice :)");
  122. console.log(art.comment("SAmmy", "Reply@", 1));
  123. console.log()
  124. console.log(art.toString('username'));
  125. console.log()
  126. art.like("Zane");
  127. console.log(art.toString('desc'));
  128.  
  129. // The result is:
  130. // John likes this article!
  131. // My Article has 0 likes
  132. // Ammy commented on My Article
  133. // You replied successfully
  134.  
  135. // Title: My Article
  136. // Creator: Anny
  137. // Likes: 0
  138. // -- 2. Ammy: New Content
  139. // -- 3. Jessy: Nice :)
  140. // -- 1. Sammy: Some Content
  141. // --- 1.2. SAmmy: Reply@
  142. // --- 1.1. Zane: Reply
  143.  
  144. // Title: My Article
  145. // Creator: Anny
  146. // Likes: 1
  147. // -- 3. Jessy: Nice :)
  148. // -- 2. Ammy: New Content
  149. // -- 1. Sammy: Some Content
  150. // --- 1.2. SAmmy: Reply@
  151. // --- 1.1. Zane: Reply
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement