-
Notifications
You must be signed in to change notification settings - Fork 44
Expand file tree
/
Copy pathindex.js
More file actions
214 lines (180 loc) · 5.03 KB
/
Copy pathindex.js
File metadata and controls
214 lines (180 loc) · 5.03 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
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
import { print } from "./js/lib.js";
("use strict");
/* 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/oleksii-anoshkin/a-tiny-JS-world
Web app: https://oleksii-anoshkin.github.io/a-tiny-JS-world/
*/
// ======== OBJECTS DEFINITIONS ========
// Constants
const INHABITANTS = [];
// Humans
const HUMAN_DATA = {
hands: 2,
legs: 2,
species: "human",
};
const MALE_DATA = {
gender: "male",
name: ["Bob", "Jonatan", "Bill"],
friends: ["Marta", "Bob", "None"],
phrases: ["Hello world!", "Hi everyone!", "Aloha!"],
icon: '<span class="material-symbols-outlined">man</span>',
};
const FEMALE_DATA = {
gender: "female",
name: ["Samanta", "Barbara", "Marta"],
friends: ["Bill", "Archie", "Leona"],
phrases: ["Ops!", "Yap!", "Okey!"],
icon: '<span class="material-symbols-outlined">woman</span>',
};
const CATWOMAN_DATA = {
name: "Julia",
friends: "Leona",
};
// Animals
const ANIMAL_DATA = {
limbs: 4,
tail: 1,
species: "animal",
icon: '<span class="material-symbols-outlined">pets</span>',
};
const DOGS_DATA = {
kind: "dog",
name: ["Archie", "Casper"],
friends: ["Barbara", "Colin"],
phrase: "Woof!",
};
const CATS_DATA = {
kind: "cat",
name: ["Colin", "Leona"],
friends: ["Casper", "Marta"],
phrase: "Meow!",
};
// HTML
const CARD_CLASS = "inhabitant__card";
const CARD_TAG = "div";
const KEY_TAG = "span";
const KEY_CLASS = "inhabitant__text";
const TEXT_CLASS = "normal";
const TEXT_TAG = "span";
const IMG_PROP_NAME = "icon";
// Classes
// General
class Inhabitant {
constructor(name, friends) {
(this.name = name), (this.friends = friends);
}
render() {
const propsArr = [];
for (const prop in this) {
prop === IMG_PROP_NAME
? propsArr.unshift(this[prop])
: propsArr.push(
`<${KEY_TAG} class="${KEY_CLASS}">${prop}: <${TEXT_TAG} class="${TEXT_CLASS}">${this[prop]}</${TEXT_TAG}></${KEY_TAG}>`
);
}
return propsArr.join(" ");
}
}
// Humans
class Human extends Inhabitant {
constructor(name, friends, phrase) {
super(name, friends);
this.saying = phrase;
this.hands = HUMAN_DATA.hands;
this.legs = HUMAN_DATA.legs;
this.species = HUMAN_DATA.species;
}
}
class Male extends Human {
constructor(name, friends, phrase) {
super(name, friends, phrase);
this.gender = MALE_DATA.gender;
this.icon = MALE_DATA.icon;
}
}
class Female extends Human {
constructor(name, friends, phrase) {
super(name, friends, phrase);
this.gender = FEMALE_DATA.gender;
this.icon = FEMALE_DATA.icon;
}
}
class Catwoman extends Female {
constructor(name, friends, phrase) {
super(name, friends, phrase);
this.gender = FEMALE_DATA.gender;
this.saying = CATS_DATA.phrase;
}
}
// Animals
class Animal extends Inhabitant {
constructor(name, friends) {
super(name, friends);
this.limbs = ANIMAL_DATA.limbs;
this.tail = ANIMAL_DATA.tail;
this.species = ANIMAL_DATA.species;
this.icon = ANIMAL_DATA.icon;
}
}
class Dog extends Animal {
constructor(name, friends) {
super(name, friends);
this.kind = DOGS_DATA.kind;
this.phrase = DOGS_DATA.phrase;
}
}
class Cat extends Animal {
constructor(name, friends) {
super(name, friends);
this.kind = CATS_DATA.kind;
this.phrase = CATS_DATA.phrase;
}
}
// Add inhabitians
function addInhabitians(className, base, dataName, dataFriends, dataPhrases) {
for (let i = 0; i < dataName.length; i += 1) {
dataPhrases === undefined
? base.push(new className(dataName[i], dataFriends[i]))
: base.push(new className(dataName[i], dataFriends[i], dataPhrases[i]));
}
}
// Add male
addInhabitians(
Male,
INHABITANTS,
MALE_DATA.name,
MALE_DATA.friends,
MALE_DATA.phrases
);
// Add female
addInhabitians(
Female,
INHABITANTS,
FEMALE_DATA.name,
FEMALE_DATA.friends,
FEMALE_DATA.phrases
);
// Add dog
addInhabitians(Dog, INHABITANTS, DOGS_DATA.name, DOGS_DATA.friends);
// Add cat
addInhabitians(Cat, INHABITANTS, CATS_DATA.name, CATS_DATA.friends);
// Add catwoman
INHABITANTS.push(new Catwoman(CATWOMAN_DATA.name, CATWOMAN_DATA.friends));
// ======== OUTPUT ========
INHABITANTS.forEach((obj) => print(`${obj.render()}`, CARD_CLASS, CARD_TAG));
/* 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.
*/
/* Print examples:
print('ABC');
print('<strong>ABC</strong>');
print('<strong>ABC</strong>', 'div');
print('human; John; male; 2; 2; Hello world!; Rex, Tom, Jenny');
print('human; <strong>John</strong>; male; 2; 2; <em>Hello world!</em>; Rex, Tom, Jenny');
print('human; <strong>John</strong>; male; 2; 2; <em>Hello world!</em>; Rex, Tom, Jenny', 'div');
*/