Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function solveClasses() {
- class Pet {
- constructor(owner, name) {
- this.owner = owner;
- this.name = name;
- this.comments = [];
- }
- addComment(comment) {
- if (this.comments.includes(comment)) {
- throw new Error('This comment is already added!');
- }
- this, this.comments.push(comment);
- return 'Comment is added.';
- }
- feed() {
- return `${this.name} is fed`;
- }
- toString() {
- const result = [];
- result.push(`Here is ${this.owner}'s pet ${this.name}.`);
- if (this.comments.length > 0) {
- result.push(`Special requirements: ${this.comments.join(', ')}`);
- }
- return result.join('\n');
- }
- }
- class Cat extends Pet {
- constructor(owner, name, insideHabits, scratching) {
- super(owner, name);
- this.insideHabits = insideHabits;
- this.scratching = scratching;
- }
- feed() {
- return `${super.feed()}, happy and purring.`;
- }
- toString() {
- const result = [];
- result.push(super.toString());
- result.push('Main information:');
- let tempResult = `${this.name} is a cat with ${this.insideHabits}`;
- if (this.scratching) {
- tempResult += ', but beware of scratches.';
- }
- result.push(tempResult);
- return result.join('\n');
- }
- }
- class Dog extends Pet {
- constructor(owner, name, runningNeeds, trainability) {
- super(owner, name);
- this.runningNeeds = runningNeeds;
- this.trainability = trainability;
- }
- feed() {
- return `${super.feed()}, happy and wagging tail.`;
- }
- toString() {
- const result = [];
- result.push(super.toString());
- result.push('Main information:');
- result.push(`${this.name} is a dog with need of ${this.runningNeeds}km running every day and ${this.trainability} trainability.`);
- return result.join('\n');
- }
- }
- return {
- Pet,
- Cat,
- Dog,
- };
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement