-
-
Notifications
You must be signed in to change notification settings - Fork 184
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
Oop exercise #416
Changes from 6 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
bb9688f
OOP_exercise task
OlexiyDobroskok 60ab98c
delete comment
OlexiyDobroskok f8e33ab
Friends list is a list of objects refs
OlexiyDobroskok 611b213
delete comments
OlexiyDobroskok 5f342e5
New Perspective on the Task
OlexiyDobroskok 21c4ad9
Tried to make a woman and a cat friends;
OlexiyDobroskok c8e3d14
Made method introduceYourSelf DRY;
OlexiyDobroskok 38a73ef
Correcting strong tag
OlexiyDobroskok 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,138 @@ | ||
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() { | ||
if (this.friends.length === 0) { | ||
this.friends = "Looking for friends!"; | ||
return `<strong>${this.saying}! My name is ${this.name}. Species: ${this.species}. Gender: ${this.gender}. Friends: ${this.friends} </strong>`; | ||
} | ||
const friendsName = this.friends.map((friend) => friend.name).join(", "); | ||
return `<strong>${this.saying}! My name is ${this.name}. Species: ${this.species}. Gender: ${this.gender}. Friends: ${friendsName}. </strong>`; | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This fragment can be made DRY. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
done |
||
} | ||
|
||
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(); |
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.
What will happen if we
.addFriend
after first call of.introduceYourSelf
?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.
@
@OleksiyRudenko Instead of an empty array, I get a string...) Done)