Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tiny js world #375

Merged
merged 12 commits into from
Oct 1, 2022
140 changes: 51 additions & 89 deletions submissions/VolodymyrRutskyi/tiny-js-world/index.js
Original file line number Diff line number Diff line change
@@ -1,90 +1,52 @@
/* Refer to https://github.com/OleksiyRudenko/a-tiny-JS-world for the task details
Complete the below for code reviewers' convenience:
class Inhabitant {
constructor(species, name, gender, legs, saying){
this.species = species;
this.name = name;
this.gender = gender;
this.legs = legs;
this.saying = saying;
this.props = ['species', 'name', 'gender', 'legs', 'saying']
}
showProps() {
return this.props.map(prop => this[prop]).join('; ');
}
}

class Human extends Inhabitant{
constructor (name, gender, saying){
super('human', name, gender, 2, saying);
this.hands = 2;
this.props = [...this.props, 'hands']
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is generally a bad practice to directly change properties that are owned by the base class.
Every class should handle its own properties i.e. encapsulate both properties and behaviour.
The same refers to Inhabitant#showProps.
Let every class handle properties it owns.
Use method overloading to provide seamless user experience.
You will benefit from explicit call to inherited version of the methos via super.
Task and original project materials offer links to the articles that you will find helpful.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have fixed

}
}
class Woman extends Human{
constructor(name, saying){
super(name, 'female', saying)
}
}
class Man extends Human{
constructor(name, saying){
super(name, 'male', saying);
}
}
class Dog extends Inhabitant{
constructor(name, gender){
super('dog', name, gender, 4, 'woof-woof')
}
}
class Cat extends Inhabitant{
constructor(name, gender){
super('cat', name, gender, 4, 'meow-meow')
}
}

const dog = new Dog('Timmy', 'male');
const cat = new Cat('Mars', 'male');
const woman = new Woman('Sarah', 'Hello, my name is Sarah!');
const man = new Man('Kevin', 'Hello, my name is Kevin!');
const inhabitants = [dog, cat, woman, man];

inhabitants.forEach((inhabitant) =>
print(inhabitant.showProps())
);

Code repository: _put repo URL here_
Web app: _put project's github pages URL here_
*/

// ======== OBJECTS DEFINITIONS ========
// Define your objects here


// ======== 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.
*/

/* 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');
*/
const dog = {
species: 'dog',
name: 'Timmy',
gender: 'male',
legs: 4,
hands: 0,
saying: 'woof-woof!'
};

const cat = {
species: 'cat',
name: 'Mars',
gender: 'male',
legs: 4,
hands: 0,
saying: 'meow-meow!'
};

const woman = {
species: 'woman',
name: 'Sarah',
gender: 'female',
legs: 2,
hands: 2,
saying: 'Hello, my name is Sarah!'
};

const man = {
species: 'man',
name: 'Kevin',
gender: 'male',
legs: 2,
hands: 2,
saying: 'Hello, my name is Kevin'
};

const catWoman = Object.create(cat);
catWoman.name = 'Cat-woman';
catWoman.species = 'human';
catWoman.gender = 'female';
catWoman.legs = 2;
catWoman.hands = 2;

const inhabitants = [dog, cat, woman, man, catWoman];

const inhabitantPropertyNames = [
'species',
'name',
'gender',
'legs',
'hands',
'saying',
];

const inhabitantDetails = inhabitants.map((inhabitant) =>
inhabitantPropertyNames.map((propName) => inhabitant[propName])
);

inhabitantDetails.forEach((inhabitant) =>
print(inhabitant.join('; '))
);