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

Oop exercise #353

Merged
merged 8 commits into from
Sep 29, 2022
73 changes: 73 additions & 0 deletions submissions/Oleksii_Drohachov/OOP-exercise/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
class Creature {
constructor(species, name, gender, saying, friends) {
this.species = species;
this.name = name;
this.gender = gender;
this.saying = saying;
this.friends = friends;
}

output() {
const { name, species, gender, saying, friends } = this;

return `${name}; ${species}; ${gender}; ${saying}; ${friends.join(", ")}`;
Copy link
Member

Choose a reason for hiding this comment

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

There is still repetition ("; "). Pack whatever you need into an array and Array#join.
You will benefit a lot from recognizing use cases for Array methods.

}
}

class Mammal extends Creature {
Copy link
Member

Choose a reason for hiding this comment

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

Mammal is the only class that extends Creature. Why do we need Mammal?

Any good reason for this?

Also, obviously there is no good reason to prefer 4 for legs. Decisions should be explainable.

constructor(species, name, gender, saying, friends) {
super(species, name, gender, saying, friends);
this.legs = 4;
}
output() {
return `${super.output()}; ${this.legs}`;
}
}

class Human extends Mammal {
constructor(name, gender, saying, friends) {
super("human", name, gender, saying, friends, 2);
this.hands = 2;
}
output() {
return `${super.output()}; ${this.hands}`;
}
}

class Dog extends Mammal {
constructor(name, gender, saying, friends) {
super("dog", name, gender, saying, friends);
}
}

class Cat extends Mammal {
constructor(name, gender, saying, friends) {
super("cat", name, gender, saying, friends);
}
}

const barbos = new Dog("Barbos", "male", "gav-gav kaje pes", [
"definitely everybody in the world !",
]);
const sonya = new Cat("Sonya", "female", "meow-meow, skinbag...", [
"definitely nobody except her own slaves",
]);
const oleksii = new Human(
"Oleksii",
"male",
"Wanna be frontend ninja in future!",
["Alex", "Victoria", "every lovely kottan on the course"]
);
const victoria = new Human(
"Victoria",
"female",
"When will you bring the money to home, honey?",
["Natasha", "Katya", "Lada"]
);
const anjela = new Human("Anjela", "female", sonya.saying, [
"definitely nobody except her own slaves, she's a cat, you know...",
]);

const tinyWorldInhabitants = [barbos, sonya, oleksii, victoria, anjela];

tinyWorldInhabitants.forEach((inhabitant) => print(inhabitant.output()));