Advertisement
Liliana797979

press house - js advanced exam

Apr 1st, 2022
1,066
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2.  
  3. function solve() {
  4.     class Article {
  5.         constructor(title, content) {
  6.             this.title = title;
  7.             this.content = content;
  8.         }
  9.  
  10.         toString() {
  11.             return `Title: ${this.title}\nContent: ${this.content}`;
  12.         }
  13.     }
  14.  
  15.     class ShortReports extends Article {
  16.         constructor(title, content, originalResearch) {
  17.             if (content.length >= 150) {
  18.                 throw new Error('Short reports content should be less then 150 symbols.');
  19.             }
  20.  
  21.             if (!originalResearch.hasOwnProperty('title') || !originalResearch.hasOwnProperty('author')) {
  22.                 throw new Error('The original research should have author and title.');
  23.             }  
  24.             super(title,content);
  25.             this.comments = [];
  26.             this.originalResearch = originalResearch;
  27.         }
  28.  
  29.         addComment(comment) {
  30.             this.comments.push(comment);
  31.             return 'The comment is added.';
  32.         }
  33.  
  34.         toString() {
  35.             let output = super.toString();
  36.             output += `\nOriginal Research: ${this.originalResearch.title} by ${this.originalResearch.author}`;
  37.  
  38.             if (this.comments.length > 0) {
  39.                 output += '\nComments:\n'
  40.                 output += this.comments.join('\n');
  41.             }
  42.  
  43.             return output;
  44.         }
  45.     }
  46.  
  47.     class BookReview extends Article {
  48.         constructor(title, content, book) {
  49.             super(title,content);
  50.             this.book = book;
  51.             this.clients = [];
  52.         }
  53.  
  54.         addClient(clientName, orderDescription) {
  55.            let hasClient =  this.clients.find(client => client.name === clientName
  56.                                             && client.description === orderDescription);
  57.  
  58.             if (hasClient) {
  59.                 throw new Error('This client has already ordered this review.');
  60.             }
  61.  
  62.             this.clients.push({name:clientName,description:orderDescription});
  63.             return `${clientName} has ordered a review for ${orderDescription}`;
  64.         }
  65.  
  66.         toString() {
  67.             let output = super.toString() +'\n';
  68.             output += `Book: ${this.book.name}`;
  69.             if (this.clients.length > 0) {
  70.                 output += '\nOrders:\n';
  71.                 output += this.clients.map(client => `${client.name} - ${client.description}`).join('\n');
  72.             }
  73.  
  74.             return output;
  75.         }
  76.     }
  77.  
  78.     return {Article, ShortReports, BookReview};
  79. }
  80.  
  81. let classes = solve();
  82. let book = new classes.BookReview("The Great Gatsby is so much more than a love story", "The Great Gatsby is in many ways similar to Romeo and Juliet, yet I believe that it is so much more than just a love story. It is also a reflection on the hollowness of a life of leisure. ...", { name: "The Great Gatsby", author: "F Scott Fitzgerald" });
  83. console.log(book.addClient("The Guardian", "100 symbols"));
  84. console.log(book.addClient("Goodreads", "30 symbols"));
  85. console.log(book.toString());
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement