-
Notifications
You must be signed in to change notification settings - Fork 44
Expand file tree
/
Copy pathindex.js
More file actions
117 lines (98 loc) · 2.71 KB
/
Copy pathindex.js
File metadata and controls
117 lines (98 loc) · 2.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
import { print } from './js/lib.js';
/* Refer to https://github.com/OleksiyRudenko/a-tiny-JS-world for the task details
Code repository: https://github.com/yazdrahobycha/a-tiny-JS-world
*/
// ======== OBJECTS DEFINITIONS ========
class Inhabitant {
constructor(name, species, gender, saying) {
this.name = name;
this.species = species;
this.gender = gender;
this.saying = saying;
}
introduce() {
return ['name', 'species', 'gender', 'saying']
.map((prop) => `${prop}: ${this[prop]}`)
.join('; ');
}
}
class Humanoid extends Inhabitant {
constructor(name, species, gender, saying) {
super(name, species, gender, saying);
this.legs = 2;
this.hands = 2;
}
introduce() {
return (
super.introduce() +
'; ' +
['legs', 'hands'].map((prop) => `${prop}: ${this[prop]}`).join('; ')
);
}
}
class HomoSapiens extends Humanoid {
constructor(name, gender, saying) {
super(name, 'human', gender, saying);
}
}
class Man extends HomoSapiens {
constructor(name, saying) {
super(name, 'male', saying);
}
}
class Women extends HomoSapiens {
constructor(name, saying) {
super(name, 'female', saying);
}
}
class Animal extends Inhabitant {
constructor(name, species, gender, saying) {
super(name, species, gender, saying);
this.legs = 4;
}
introduce() {
return super.introduce() + '; legs: ' + this.legs + '; ';
}
}
class Dog extends Animal {
constructor(name, gender) {
super(name, 'dog', gender, Dog.saying());
}
static saying() {
return 'Woof!';
}
}
class Cat extends Animal {
constructor(name, gender) {
super(name, 'cat', gender, Cat.saying());
}
static saying() {
return 'Meow!';
}
}
class CatWoman extends Humanoid {
constructor(name) {
super(name, 'cat-women', 'female', Cat.saying());
}
}
const man = new Man('Grisha', 'Bruh!');
const woman = new Women('Liza', 'Iu!');
const dog = new Dog('Dina', 'female');
const cat = new Cat('Pukch', 'male');
const catWoman = new CatWoman('Sasha');
const friendships = [
{ inhabitant: man, friends: [dog, catWoman] },
{ inhabitant: woman, friends: [cat, catWoman] },
{ inhabitant: dog, friends: [man, woman] },
{ inhabitant: cat, friends: [] },
{ inhabitant: catWoman, friends: [man, cat] },
];
friendships.forEach(({ inhabitant, friends }) => {
print(
`<i>${inhabitant.introduce()}; friends: ${
friends.length > 0
? friends.map(({ name }) => name).join(', ')
: `No friends yet:(`
}</i>`
);
});