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 #416

Merged
merged 8 commits into from
Sep 26, 2022
Merged
Changes from all 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
146 changes: 146 additions & 0 deletions submissions/OlexiyDobroskok/OOP_exercise/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
/* 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/OlexiyDobroskok/a-tiny-JS-world
Web app: https://olexiydobroskok.github.io/a-tiny-JS-world/
*/

// ======== OBJECTS DEFINITIONS ========
class Inhabitant {
constructor({
species = "No species",
name = "No name",
gender = "No gender",
saying = "Hello",
friends = [],
}) {
this.species = species;
this.name = name;
this.gender = gender;
this.saying = saying;
this.friends = friends;
}

addFriend(person) {
this.friends.push(person);
}

introduceYourSelf() {
const template = `<strong>${this.saying}! My name is ${this.name}. Species: ${this.species}. Gender: ${this.gender}.</strong>`;
if (this.friends.length !== 0) {
const friendsName = this.friends.map((friend) => friend.name).join(", ");
return template + `<strong>Friends: ${friendsName}.</strong>`;
}
return template + `<strong>Friends: Looking for friends!</strong>`;
Comment on lines +29 to +34
Copy link
Member

Choose a reason for hiding this comment

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

You have something for any situation about friends (0 or non-zero number of friends).
const friendsName = this.friends.length ? this.friends.map((friend) => friend.name).join(", "); : "Looking for friends"
Now you can have a single return statement. Altogether 2 lines of code for this method.
Feel free searching for ternary operators and/or seek for explanation in Students' chat.

Ternaries are helpful, you will use them often. Worth mastering them.

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 Thanks. Looks cool) Need to look at ternary operators.

}
}

class Human extends Inhabitant {
constructor({ name, gender, saying, legs = 2, hands = 2, friends }) {
super({ species: "human", name, gender, saying, friends });
this.hands = hands;
this.legs = legs;
}
introduceYourSelf() {
return (
super.introduceYourSelf() +
`<strong>I have ${this.hands} hands and ${this.legs} legs!</strong>`
);
}
}

class Man extends Human {
constructor({ name, saying, friends, legs, hands }) {
super({ name, gender: "male", saying, legs, hands, friends });
}
}

class Woman extends Human {
constructor({ name, saying, friends, legs, hands }) {
super({ name, gender: "female", saying, legs, hands, friends });
}
}

class Pets extends Inhabitant {
constructor({ species, name, gender, paws = 4, saying, friends }) {
super({ species, name, gender, saying, friends });
this.paws = paws;
}
introduceYourSelf() {
return (
super.introduceYourSelf() + `<strong>I have ${this.paws} paws!</strong>`
);
}
}

class Cat extends Pets {
static meow;
constructor({ name, gender, saying = "¡miau miau", friends, paws }) {
super({
species: "cat",
name,
gender,
saying,
paws,
friends,
});
Cat.meow = saying;
this.saying = Cat.getMeow();
}
static getMeow() {
return this.meow;
}
}

class Dog extends Pets {
static bark;
constructor({ name, gender, saying = "¡guau guau", friends, paws }) {
super({
species: "dog",
name,
gender,
saying,
paws,
friends,
});
Dog.bark = this.saying;
}
}

class CatWoman extends Woman {
constructor({ name, saying = "", friends, legs, hands }) {
super({ name, saying, friends, legs, hands });
this.saying = Cat.getMeow() + " " + saying;
}
}

const manJose = new Man({ name: "José", saying: "Hola, amigo" });
const womanMartina = new Woman({ name: "Martina", saying: "Buenos días" });
const catLalo = new Cat({ name: "Lalo", gender: "female" });
const dogPako = new Dog({ name: "Pako", gender: "male" });
const catwomanNerea = new CatWoman({ name: "Nerea", saying: "muchachos" });

manJose.addFriend(catwomanNerea);
womanMartina.addFriend(dogPako);
catwomanNerea.addFriend(catLalo);
catwomanNerea.addFriend(manJose);

const worldPopulation = [
manJose,
womanMartina,
catLalo,
dogPako,
catwomanNerea,
];

function showWorldPopulation() {
const inhabitantNames = worldPopulation.map((inhabitant) => inhabitant.name);
print(
`<strong>Population: ${worldPopulation.length} inhabitants! It is: ${inhabitantNames}</strong>`
);
worldPopulation.forEach((inhabitant) =>
print(inhabitant.introduceYourSelf())
);
}

showWorldPopulation();