Advertisement
Guest User

Posts

a guest
Nov 4th, 2016
599
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.  
  8.         toString() {
  9.             return `Post: ${this.title}\nContent: ${this.content}`;
  10.         }
  11.     }
  12.  
  13.     class SocialMediaPost extends Post {
  14.         constructor(title, content, likes, dislikes) {
  15.             super(title, content);
  16.             this.likes = likes;
  17.             this.dislikes = dislikes;
  18.             this.comments = [];
  19.         }
  20.  
  21.         addComment(comment) {
  22.             this.comments.push(comment);
  23.         }
  24.  
  25.         toString() {
  26.             let superString = super.toString();
  27.             let rating = this.likes - this.dislikes;
  28.  
  29.             if (this.comments.length > 0) {
  30.                 let commentsToPrint = '';
  31.                 for (let comment of this.comments) {
  32.                     commentsToPrint += `\n * ${comment}`;
  33.                 }
  34.  
  35.                 return `${superString}\nRating: ${rating}\nComments: ${commentsToPrint}`;
  36.             } else {
  37.                 return `${superString}\nRating: ${rating}`;
  38.             }
  39.  
  40.         }
  41.     }
  42.  
  43.     class BlogPost extends Post {
  44.         constructor(title, content, views) {
  45.             super(title, content);
  46.             this.views = views;
  47.         }
  48.  
  49.         view() {
  50.             this.views++;
  51.             return this;
  52.         }
  53.  
  54.         toString() {
  55.             let superString = super.toString();
  56.  
  57.             return superString + `\nViews: ${this.views}`;
  58.         }
  59.     }
  60.  
  61.     return {Post, SocialMediaPost, BlogPost}
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement