Advertisement
Marin171

Posts

Jun 14th, 2023
19
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.26 KB | None | 0 0
  1. function solution() {
  2. class Post {
  3. constructor(title, content) {
  4. this.title = title;
  5. this.content = content;
  6. }
  7.  
  8. toString() {
  9. return `Post: ${this.title}\n` + `Content: ${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. return super.toString() + '\n' +
  27. `Rating: ${this.likes - this.dislikes}` + (this._comments.length ? '\nComments:' + '\n' +
  28. (this._comments.map(c => ` * ${c}`).join('\n')) : '');
  29. }
  30. }
  31.  
  32. class BlogPost extends Post {
  33. constructor(title, content, views) {
  34. super(title, content);
  35. this.views = views;
  36. }
  37.  
  38. view() {
  39. this.views++;
  40. return this;
  41. }
  42.  
  43. toString() {
  44. return super.toString() + '\n' + `Views: ${this.views}`;
  45. }
  46. }
  47.  
  48. return {Post, SocialMediaPost, BlogPost};
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement