Advertisement
Nikolcho

IVAN2

Jun 27th, 2020
1,519
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solveClasses() {
  2.     class Pet {
  3.         constructor(owner, name) {
  4.             this.owner = owner;
  5.             this.name = name;
  6.             this.comments = [];
  7.         }
  8.  
  9.         addComment(comment) {
  10.             this.comments.forEach(c => {
  11.                 if (c === comment) {
  12.                     throw new Error('This comment is already added!');
  13.                 }
  14.             });
  15.             this.comments.push(comment);
  16.            
  17.             return 'Comment is added.';
  18.         }
  19.  
  20.         feed() {
  21.             return `${this.name} is fed`;
  22.         }
  23.  
  24.         toString() {
  25.             let result = `Here is ${this.owner}'s pet ${this.name}.`
  26.            if (this.comments.length > 0) {
  27.                result += `\nSpecial requirements: ${this.comments.join(', ')}`;
  28.            }
  29.            return result;
  30.        }
  31.    }
  32.  
  33.    class Cat extends Pet {
  34.        constructor( owner, name, insideHabits, scratching ) {
  35.            super(owner, name);
  36.            this.insideHabits = insideHabits;
  37.            this.scratching = scratching;
  38.        }
  39.  
  40.        feed() {
  41.            return super.feed() + ', happy and purring.';
  42.        }
  43.  
  44.        toString() {
  45.            let result = super.toString() + '\n' + `Main information:\n${this.name} is a cat with ${this.insideHabits}`;
  46.            if (this.scratching) {
  47.                result += ', but beware of scratches.';
  48.            }
  49.            return result;
  50.        }
  51.    }
  52.  
  53.    class Dog extends Pet {
  54.        constructor(owner, name, runningNeeds, trainability) {
  55.            super(owner, name);
  56.            this.runningNeeds = runningNeeds;
  57.            this.trainability = trainability;
  58.        }
  59.  
  60.        feed() {
  61.            return super.feed() + ', happy and wagging tail.';
  62.        }
  63.  
  64.        toString() {
  65.            let result = super.toString() + '\n' + 'Main information:\n';
  66.            result += `${this.name} is a dog with need of ${this.runningNeeds}km running every day and ${this.trainability} trainability.`;
  67.            return result;
  68.        }
  69.    }
  70.  
  71.    return {
  72.        Pet,
  73.        Cat,
  74.        Dog
  75.    }
  76. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement