kstoyanov

02. Article -23 February 2020- exam

Oct 19th, 2020 (edited)
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class Article {
  2.     #comments = [];
  3.     #likes = [];
  4.     #commentId = 0;
  5.  
  6.     constructor(title, creator) {
  7.         this.title = title;
  8.         this.creator = creator;
  9.     }
  10.  
  11.     get likes() {
  12.         if (this.#likes.length === 0) {
  13.             return `${this.title} has 0 likes`;
  14.         }
  15.         else if (this.#likes.length === 1) {
  16.             return `${this.#likes[0]} likes this article!`;
  17.         }
  18.         else {
  19.             return `${this.#likes[0]} and ${this.#likes.length - 1} others like this article!`;
  20.         }
  21.     }
  22.  
  23.     like(username) {
  24.         if (this.creator === username) {
  25.             throw new Error(`You can't like your own articles!`);
  26.        }
  27.  
  28.        if (this.#likes.includes(username)) {
  29.            throw new Error(`You can't like the same article twice!`);
  30.         }
  31.  
  32.         this.#likes.push(username);
  33.  
  34.         return `${username} liked ${this.title}!`;
  35.     }
  36.  
  37.     dislike(username) {
  38.         if (!this.#likes.includes(username)) {
  39.             throw new Error(`You can't dislike this article!`);
  40.        }
  41.  
  42.        this.#likes = this.#likes.filter(x => x !== username);
  43.        return `${username} disliked ${this.title}`;
  44.    }
  45.  
  46.    comment(username, content, id) {
  47.        let comment = this.#comments.find(c => c.id === id);
  48.  
  49.        if (!comment) {
  50.            comment = {
  51.                id: ++this.#commentId,
  52.                username,
  53.                content,
  54.                replies: []
  55.            }
  56.            this.#comments.push(comment);
  57.  
  58.            return `${username} commented on ${this.title}`;
  59.        }
  60.  
  61.        const reply = {
  62.            id: comment.replies.length + 1,
  63.            username,
  64.            content
  65.        }
  66.        comment.replies.push(reply);
  67.  
  68.        return `You replied successfully`;
  69.    }
  70.  
  71.    toString(sortingType) {
  72.        const result = [];
  73.        result.push(`Title: ${this.title}\nCreator: ${this.creator}\nLikes: ${this.#likes.length}\nComments:`);
  74.  
  75.        if(sortingType === 'username') {
  76.            this.#comments = this.#comments.sort((a, b) => a.username.localeCompare(b.username));
  77.            this.#comments.forEach(comment => {
  78.                result.push(`-- ${comment.id}. ${comment.username}: ${comment.content}`);
  79.                
  80.                comment.replies = comment.replies.sort((a, b) => a.username.localeCompare(b.username));
  81.                comment.replies.forEach(replay => {
  82.                    result.push(`--- ${comment.id}.${replay.id}. ${replay.username}: ${replay.content}`);
  83.                });
  84.            });
  85.        }
  86.  
  87.        if(sortingType === 'asc') {
  88.            this.#comments = this.#comments.sort((a, b) => a.id - b.id);
  89.            this.#comments.forEach(comment => {
  90.                result.push(`-- ${comment.id}. ${comment.username}: ${comment.content}`);
  91.                
  92.                comment.replies = comment.replies.sort((a, b) => a.id - b.id);
  93.                comment.replies.forEach(replay => {
  94.                    result.push(`--- ${comment.id}.${replay.id}. ${replay.username}: ${replay.content}`);
  95.                });
  96.            });
  97.        }
  98.  
  99.        if(sortingType === 'desc') {
  100.            this.#comments = this.#comments.sort((a, b) => b.id - a.id);
  101.            this.#comments.forEach(comment => {
  102.                result.push(`-- ${comment.id}. ${comment.username}: ${comment.content}`);
  103.                
  104.                comment.replies = comment.replies.sort((a, b) => b.id - a.id);
  105.                comment.replies.forEach(replay => {
  106.                    result.push(`--- ${comment.id}.${replay.id}. ${replay.username}: ${replay.content}`);
  107.                });
  108.            });
  109.        }
  110.  
  111.        return result.join('\n');
  112.    }
  113. }
  114.  
Add Comment
Please, Sign In to add comment