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 oop #378

Merged
merged 3 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
88 changes: 88 additions & 0 deletions submissions/bmukha/tiny-js-world-oop/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/* 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/bmukha/a-tiny-JS-world
Web app: https://bmukha.github.io/a-tiny-JS-world/
*/

class Character {
constructor({ specie, name, gender, legs = 2, saying, friends = [] }) {
this.specie = specie;
this.name = name;
this.gender = gender;
this.legs = legs;
this.saying = saying;
this.friends = friends;
}

getPresentationString() {
return ['specie', 'name', 'gender', 'legs', 'saying', 'friends']
.map((prop) =>
Array.isArray(this[prop]) ? this[prop].join(', ') : this[prop]
)
.filter((value) => Boolean(value))
Copy link
Member

Choose a reason for hiding this comment

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

You may want to filter values before converting 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.

I'm a bit confused about this one. In the previous version of this task, I deliberately rewrote that piece this way after your suggestions. And it really simplified my code because I can get rid of all the falsy values with one filter (because I convert possible empty arrays (still truthy values) into empty strings (falsy values) beforehand. Here are the links to our discussion:
Your suggestion
My reply
So, unless I'm missing something, I guess it is better to stick to this realisation. What do you think?

Copy link
Member

Choose a reason for hiding this comment

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

Oh, I see. Makes sense.
I was thinking of rather null or undefined to denote no friends situation.
Empty array is a valid approach. Keep this fragment as it is.

.join('; ');
}
}

class Dog extends Character {
constructor({ name, gender, saying, friends }) {
super({ specie: 'dog', name, gender, legs: 4, saying, friends });
}
}

const beethoven = new Dog({
name: 'Beethoven',
gender: 'male',
saying: 'woof',
friends: ['Clyde'],
});

class Cat extends Character {
constructor({ name, gender, saying, friends }) {
super({ specie: 'cat', name, gender, legs: 4, saying, friends });
}
}

const grizabella = new Cat({
name: 'Grizabella',
gender: 'female',
saying: 'meow',
friends: ['Bonnie'],
});

class Human extends Character {
constructor({ name, gender, saying, friends }) {
super({ specie: 'human', name, gender, saying, friends });
this.hands = 2;
}
getPresentationString() {
return `${super.getPresentationString()}; ${this.hands}`;
}
}

const clyde = new Human({
name: 'Clyde',
gender: 'male',
saying: 'Get rich or die trying!',
friends: ['Bonnie', 'Beethoven'],
});

const bonnie = new Human({
name: 'Bonnie',
gender: 'female',
saying: 'I have the right to not answer a questions!',
friends: ['Clyde', 'Grizabella'],
});

class CatWomen extends Human {
constructor({ name, friends }) {
super({ name, gender: 'female', saying: grizabella.saying, friends });
}
}

const selina = new CatWomen({ name: 'Selina' });

[beethoven, grizabella, clyde, bonnie, selina].forEach((character) =>
print(character.getPresentationString())
);