forked from OleksiyRudenko/a-tiny-JS-world
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
192 lines (168 loc) · 4.34 KB
/
index.js
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
/* Refer to https://github.com/OleksiyRudenko/a-tiny-JS-world for the task details
Complete the below for code reviewers' convenience:
Code repository: https://github.com/m-ruslan/a-tiny-JS-world
Web app: https://m-ruslan.github.io/a-tiny-JS-world/
*/
// ======== OBJECTS DEFINITIONS ========
// Define your objects here
//**ES6 classes and sub-classes
class Inhabitant {
constructor(name, legs, gender, propNamesArr) {
this.name = name;
this.gender = gender;
this.legs = legs;
this.friendTo = [];
this.propNamesArr = propNamesArr;
}
addFriends(friendsArr) {
this.friendTo = this.friendTo.concat(
friendsArr.map((inhhabitant) => inhhabitant.name)
);
}
makeMessage() {
return this.propNamesArr
.map((propName) =>
typeof this[propName] === "function"
? `${propName}: ${this[propName]()}`
: `${propName}: ${this[propName]}`
)
.join(";\n");
}
}
class Human extends Inhabitant {
constructor(name, legs, hands, gender) {
const propNamesArr = [
"species",
"name",
"saying",
"gender",
"legs",
"hands",
"friendTo",
];
super(name, legs, gender, propNamesArr);
this.hands = hands;
this.species = "human";
}
}
class Man extends Human {
constructor(name, legs, hands, gender = "male") {
super(name, legs, hands, gender);
}
saying() {
return `Hi, I'm ${this.name}`;
}
}
class Woman extends Human {
constructor(name, legs, hands, gender = "female") {
super(name, legs, hands, gender);
}
saying() {
return `Hello, I'm ${this.name}`;
}
}
class Pet extends Inhabitant {
constructor(name, legs, gender) {
const propNamesArr = [
"species",
"name",
"saying",
"gender",
"legs",
"friendTo",
];
super(name, legs, gender, propNamesArr);
}
}
class Dog extends Pet {
constructor(name, legs, gender) {
super(name, legs, gender);
this.species = "dog";
}
saying() {
return `Woof, I'm ${this.name}`;
}
}
//**classes built employing composition
const sayingMoew = (state) => ({
saying: () => `Meow, I'm ${state.name}`,
});
const addFriends = (state) => ({
addFriends: (friendsArr) =>
(state.friendTo = state.friendTo.concat(
friendsArr.map((inhhabitant) => inhhabitant.name)
)),
});
const makeMessage = (state) => ({
makeMessage: () =>
state.propNamesArr
.map((propName) =>
typeof state[propName] === "function"
? `${propName}: ${state[propName]()}`
: `${propName}: ${state[propName]}`
)
.join(";\n"),
});
const Cat = (name, legs, gender) => {
let state = {
name,
friendTo: [],
gender,
species: "cat",
legs,
propNamesArr: ["species", "name", "saying", "gender", "legs", "friendTo"],
};
return Object.assign(
state,
sayingMoew(state),
addFriends(state),
makeMessage(state)
);
};
const CatWoman = (name, legs, hands, gender = "female") => {
let state = {
name,
friendTo: [],
gender,
species: "catwoman",
legs,
hands,
propNamesArr: [
"species",
"name",
"saying",
"gender",
"legs",
"hands",
"friendTo",
],
};
return Object.assign(
state,
sayingMoew(state),
addFriends(state),
makeMessage(state)
);
};
//**objects creation based on classes
const dog = new Dog("Ghost", 4, "male");
const woman = new Woman("Monica", 2, 2);
const man = new Man("Chandler", 2, 2);
const cat = Cat("Tom", 4, "male");
const catWoman = CatWoman("Cat-woman", 2, 2);
const friendList = [
{ target: dog, friends: [man, woman] },
{ target: man, friends: [woman, cat] },
{ target: woman, friends: [dog, man, cat] },
{ target: cat, friends: [dog, man, woman] },
{ target: catWoman, friends: [man, cat] },
].forEach(({ target, friends }) => target.addFriends(friends));
// ======== OUTPUT ========
/* Use print(message) for output.
Default tag for message is <pre>. Use print(message,'div') to change containing element tag.
Message can contain HTML markup. You may also tweak index.html and/or styles.css.
However, please, REFRAIN from improving visuals at least until your code is reviewed
so code reviewers might focus on a single file that is index.js.
*/
const world = [dog, cat, woman, man, catWoman];
world.forEach((inhabitant) => print(inhabitant.makeMessage()));