-
-
Notifications
You must be signed in to change notification settings - Fork 183
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
tiny js world oop #378
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
.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()) | ||
); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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
orundefined
to denoteno friends
situation.Empty array is a valid approach. Keep this fragment as it is.