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

New index.js (tiny JS) for OOP Exercise #342

Merged
merged 7 commits into from
Oct 9, 2022
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 77 additions & 0 deletions submissions/Levi123/oop-exercise/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
class Entity {
Copy link
Member

Choose a reason for hiding this comment

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

entity is a generic term. Business domain and task requirements are a good source of naming ideas. Name should describe what entity it represents at a given level of abstraction - dog, cat, human, mammal, herbivore - in the given business domain.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

constructor (name, species, gender, saying){
this.name = name;
this.species = species;
this.gender = gender;
this.saying = saying;
}
}

class Human extends Entity {
constructor (name, gender, saying){
super(name, 'Human', gender, saying);
this.legs = 2;
this.hands = 2;
}
}

class Woman extends Human {
constructor(name, saying){
super(name, 'Woman', saying)
Copy link
Member

Choose a reason for hiding this comment

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

Men and women are the same species.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, a man and a woman get one species - a person. But it has a gender property to display gender, if that's what you're after.

}
}

class Animal extends Entity {
constructor(name, gender, saying){
super(name, 'Animal', gender, saying);
Copy link
Member

Choose a reason for hiding this comment

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

"Animal" is not a species.

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 changed the name of the main parent class entity to mammal. The animal class has a species - animals, and a person - human species I think this is not a mistake, but simply a manifestation of fantasy in this task. We do not receive a clear structure that should be followed.

this.paws = 4;
}
}

class Man extends Human {
constructor(name, saying){
super(name, 'Man', saying)
}
}

class CatWoman extends Woman {
constructor(name){
super(name, cat.saying);
}
}

class Dog extends Animal {
constructor(name, gender, saying){
super(name, gender, saying)
}
}

class Cat extends Animal {
constructor(name, gender, saying){
super(name, gender, saying)
}
}

const woman = new Woman('Olivia', 'Hello!')
const cat = new Cat('Bonya', 'Girl', 'Meeeeow!')
const dog = new Dog('Dollar', 'Boy', 'Gav-gav!')
const man = new Man('Johny', 'Hellllllooooo!')
const catWoman = new CatWoman('Kitty')

const allResidents = [woman, man, dog, cat, catWoman]
const allProp = ['name', 'species', 'gender', 'saying', 'legs', 'hands', 'paws']


function toPrint (arrayAllResidents) {
const propToPrint = allProp.filter((propOfObj) => arrayAllResidents[propOfObj] !== undefined)
.map((propOfObj) => arrayAllResidents[propOfObj]).join('; ');
print(propToPrint);
}
Copy link
Member

Choose a reason for hiding this comment

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

This approach was good enough before OOP.
Following the OOP paradigm classes encapsulate properties and behaviour (properties handling).
Every class handles props it owns immediately.
Method overloading and explicit call to inherited method version via super methods are to the rescue.
Task and original project docs offer links to the materials you will find helpful in these regards.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This approach was good enough before OOP. Following the OOP paradigm classes encapsulate properties and behaviour (properties handling). Every class handles props it owns immediately. Method overloading and explicit call to inherited method version via super methods are to the rescue. Task and original project docs offer links to the materials you will find helpful in these regards.

done


function printAllObjects(arrayAllObjects){
arrayAllObjects.forEach((characters) => {
toPrint(characters);
Copy link
Member

Choose a reason for hiding this comment

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

Does forEach receive multiple characters or a single character on every iteration?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@OleksiyRudenko single character on every iteration

})
}

printAllObjects(allResidents);