Advertisement
dilyana2001

Untitled

Oct 17th, 2021
65
0
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.         }
  13.  
  14.         if (this._likes.length == 1) {
  15.             return `${this._likes[0]} likes this story!`;
  16.         }
  17.  
  18.         return `${this._likes[0]} and ${this._likes.length - 1} others like this story!`;
  19.  
  20.     }
  21.  
  22.     like(username) {
  23.         if (this._likes.includes(username)) {
  24.             throw new Error(`You can't like the same story twice`);
  25.        }
  26.  
  27.        if (username == this.creator) {
  28.            throw new Error(`You can't like your own story!`);
  29.         }
  30.  
  31.         this._likes.push(username);
  32.         return `${username} liked ${this.title}!`;
  33.     }
  34.  
  35.     dislike(username) {
  36.         if (!this._likes.includes(username)) {
  37.             throw new Error(`You can't dislike this story!`);
  38.        }
  39.        let index = this._likes.indexOf(username);
  40.        this._likes.splice(index, 1)
  41.        return `${username} disliked ${this.title}`;
  42.    }
  43.  
  44.    comment(username, content, id) {
  45.        if (id == undefined || !this._comments.some(x => x.Id == id)) {
  46.            this._comments.push({
  47.                Id: this._comments.length + 1,
  48.                Username: username,
  49.                Content: content,
  50.                Replies: []
  51.            });
  52.            return `${username} commented on ${this.title}`
  53.        }
  54.        let commentToReplay = this._comments.find(x => x.Id == id);
  55.        commentToReplay.Replies.push({
  56.            Id: `${commentToReplay.Id}.${commentToReplay.Replies.length + 1}`,
  57.            Username: username,
  58.            Content: content
  59.        })
  60.        return `You replied successfully`
  61.    }
  62.  
  63.    toString(sortingType) {
  64.        let sortValue = {
  65.            asc: (a, b) => a.Id - b.Id,
  66.            desc: (a, b) => b.Id - a.Id,
  67.            username: (a, b) => a.Username.localeCompare(b.Username),
  68.        }
  69.  
  70.        let comments = this._comments.sort(sortValue[sortingType]);
  71.        comments.forEach(x => x.Replies.sort(sortValue[sortingType]));
  72.  
  73.        let result = [`Title: ${this.title}\nCreator: ${this.creator}\nLikes: ${this._likes.length}\nComments:\n`];
  74.        comments.forEach(x => {
  75.            result.push(`-- ${x.Id}. ${x.Username}: ${x.Content}\n`);
  76.            x.Replies.forEach(y => {
  77.                result.push(`--- ${y.Id}. ${y.Username}: ${y.Content}\n`);
  78.            })
  79.        })
  80.        return result.join('');
  81.    }
  82. }
  83.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement