Advertisement
kstoyanov

04. Posts

Oct 16th, 2020
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve() {
  2.     class Post {
  3.         constructor(title, content) {
  4.             this.title = title;
  5.             this.content = content;
  6.         }
  7.         toString(){
  8.             return `Post: ${this.title}\nContent: ${this.content}`;
  9.         }
  10.     }
  11.  
  12.     class SocialMediaPost extends Post {
  13.         constructor(title, content, likes, dislikes) {
  14.             super(title, content);
  15.             this.likes = likes;
  16.             this.dislikes = dislikes;
  17.             this.comments = []
  18.         }
  19.  
  20.         addComment(comment) {
  21.             this.comments.push(comment);
  22.         }
  23.  
  24.         toString() {
  25.             let result = super.toString() + `\nRating: ${this.likes - this.dislikes}`;
  26.             if (this.comments.length > 0) {
  27.                 result += '\nComments:';
  28.                 this.comments.forEach(comment => result += `\n * ${comment}`);
  29.             }
  30.             return result;
  31.         }
  32.     }
  33.  
  34.     class BlogPost extends Post {
  35.         constructor(title, content, views) {
  36.             super(title, content);
  37.             this.views = views;
  38.         }
  39.  
  40.         view() {
  41.             this.views++;
  42.             return this;
  43.         }
  44.  
  45.         toString() {
  46.             return super.toString() + `\nViews: ${this.views}`;
  47.         }
  48.     }
  49.  
  50.     return {
  51.         Post,
  52.         SocialMediaPost,
  53.         BlogPost
  54.     }
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement