-
-
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
Tiny js world #375
Merged
Merged
Tiny js world #375
Changes from 8 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
b7af201
First approach
VladimirRutskiy f9e8889
Improved code according to OOP
VladimirRutskiy 86bc054
Update index.js
VladimirRutskiy ea7c491
Merge branch 'main' into Tiny_js_world
VladimirRutskiy 4327e3a
Added full index file
VladimirRutskiy c0a923e
Update index.js
VladimirRutskiy 6a217ba
Fixed some remarks
VladimirRutskiy 1d2a2c3
Fixed some remarks
VladimirRutskiy 2b9e2ef
Fixed some remarks
VladimirRutskiy 32370ee
Update index.js
VladimirRutskiy e0fb4ff
fixed some remarks
VladimirRutskiy c61dba5
fixes some mistakes
VladimirRutskiy 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 |
---|---|---|
@@ -1,90 +1,52 @@ | ||
/* Refer to https://github.com/OleksiyRudenko/a-tiny-JS-world for the task details | ||
Complete the below for code reviewers' convenience: | ||
class Inhabitant { | ||
constructor(species, name, gender, legs, saying){ | ||
this.species = species; | ||
this.name = name; | ||
this.gender = gender; | ||
this.legs = legs; | ||
this.saying = saying; | ||
this.props = ['species', 'name', 'gender', 'legs', 'saying'] | ||
} | ||
showProps() { | ||
return this.props.map(prop => this[prop]).join('; '); | ||
} | ||
} | ||
|
||
class Human extends Inhabitant{ | ||
constructor (name, gender, saying){ | ||
super('human', name, gender, 2, saying); | ||
this.hands = 2; | ||
this.props = [...this.props, 'hands'] | ||
} | ||
} | ||
class Woman extends Human{ | ||
constructor(name, saying){ | ||
super(name, 'female', saying) | ||
} | ||
} | ||
class Man extends Human{ | ||
constructor(name, saying){ | ||
super(name, 'male', saying); | ||
} | ||
} | ||
class Dog extends Inhabitant{ | ||
constructor(name, gender){ | ||
super('dog', name, gender, 4, 'woof-woof') | ||
} | ||
} | ||
class Cat extends Inhabitant{ | ||
constructor(name, gender){ | ||
super('cat', name, gender, 4, 'meow-meow') | ||
} | ||
} | ||
|
||
const dog = new Dog('Timmy', 'male'); | ||
const cat = new Cat('Mars', 'male'); | ||
const woman = new Woman('Sarah', 'Hello, my name is Sarah!'); | ||
const man = new Man('Kevin', 'Hello, my name is Kevin!'); | ||
const inhabitants = [dog, cat, woman, man]; | ||
|
||
inhabitants.forEach((inhabitant) => | ||
print(inhabitant.showProps()) | ||
); | ||
|
||
Code repository: _put repo URL here_ | ||
Web app: _put project's github pages URL here_ | ||
*/ | ||
|
||
// ======== OBJECTS DEFINITIONS ======== | ||
// Define your objects here | ||
|
||
|
||
// ======== OUTPUT ======== | ||
/* Use print(message) for output. | ||
Default tag for message is <pre>. Use print(message,'div') to change containing element tag. | ||
|
||
Message can contain HTML markup. You may also tweak index.html and/or styles.css. | ||
However, please, REFRAIN from improving visuals at least until your code is reviewed | ||
so code reviewers might focus on a single file that is index.js. | ||
*/ | ||
|
||
/* Print examples: | ||
print('ABC'); | ||
print('<strong>ABC</strong>'); | ||
print('<strong>ABC</strong>', 'div'); | ||
|
||
print('human; John; male; 2; 2; Hello world!; Rex, Tom, Jenny'); | ||
print('human; <strong>John</strong>; male; 2; 2; <em>Hello world!</em>; Rex, Tom, Jenny'); | ||
print('human; <strong>John</strong>; male; 2; 2; <em>Hello world!</em>; Rex, Tom, Jenny', 'div'); | ||
*/ | ||
const dog = { | ||
species: 'dog', | ||
name: 'Timmy', | ||
gender: 'male', | ||
legs: 4, | ||
hands: 0, | ||
saying: 'woof-woof!' | ||
}; | ||
|
||
const cat = { | ||
species: 'cat', | ||
name: 'Mars', | ||
gender: 'male', | ||
legs: 4, | ||
hands: 0, | ||
saying: 'meow-meow!' | ||
}; | ||
|
||
const woman = { | ||
species: 'woman', | ||
name: 'Sarah', | ||
gender: 'female', | ||
legs: 2, | ||
hands: 2, | ||
saying: 'Hello, my name is Sarah!' | ||
}; | ||
|
||
const man = { | ||
species: 'man', | ||
name: 'Kevin', | ||
gender: 'male', | ||
legs: 2, | ||
hands: 2, | ||
saying: 'Hello, my name is Kevin' | ||
}; | ||
|
||
const catWoman = Object.create(cat); | ||
catWoman.name = 'Cat-woman'; | ||
catWoman.species = 'human'; | ||
catWoman.gender = 'female'; | ||
catWoman.legs = 2; | ||
catWoman.hands = 2; | ||
|
||
const inhabitants = [dog, cat, woman, man, catWoman]; | ||
|
||
const inhabitantPropertyNames = [ | ||
'species', | ||
'name', | ||
'gender', | ||
'legs', | ||
'hands', | ||
'saying', | ||
]; | ||
|
||
const inhabitantDetails = inhabitants.map((inhabitant) => | ||
inhabitantPropertyNames.map((propName) => inhabitant[propName]) | ||
); | ||
|
||
inhabitantDetails.forEach((inhabitant) => | ||
print(inhabitant.join('; ')) | ||
); |
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.
It is generally a bad practice to directly change properties that are owned by the base class.
Every class should handle its own properties i.e. encapsulate both properties and behaviour.
The same refers to
Inhabitant#showProps
.Let every class handle properties it owns.
Use method overloading to provide seamless user experience.
You will benefit from explicit call to inherited version of the methos via
super
.Task and original project materials offer links to the articles that you will find helpful.
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 have fixed