Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

add tiny-js-world homework #193

Merged
merged 3 commits into from
Aug 18, 2022
Merged
Changes from 1 commit
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
82 changes: 82 additions & 0 deletions submissions/bmukha/tiny-js-world/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/* 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/
*/

const dog = {
species: 'dog',
name: 'Beethoven',
gender: 'male',
legs: 4,
hands: 0,
saying: 'woof',
friends: ['Clyde'],
};

const cat = {
species: 'cat',
name: 'Grizabella',
gender: 'female',
legs: 4,
hands: 0,
saying: 'meow',
friends: ['Bonnie'],
};

const man = {
species: 'human',
name: 'Clyde',
gender: 'male',
legs: 2,
hands: 2,
saying: 'Get rich or die trying!',
friends: ['Bonnie', 'Beethoven'],
};

const woman = {
species: 'human',
name: 'Bonnie',
gender: 'female',
legs: 2,
hands: 2,
saying: 'I have the right to not answer a questions!',
friends: ['Clyde', 'Grizabella'],
};

const catwoman = {
species: 'human',
name: 'Selina',
gender: 'female',
legs: 2,
hands: 2,
saying: cat.saying,
friends: [],
};

const characters = [dog, cat, man, woman, catwoman];

const props = [
'species',
'name',
'gender',
'legs',
'hands',
'saying',
'friends',
];

characters.forEach((character) => {
let representation = '';
for (let i = 0; i < props.length; i++) {
if (props[i] === 'hands' && character[props[i]] === 0) {
continue;
} else if (i < props.length - 1) {
representation += `${character[props[i]]}; `;
} else {
representation += `${character[props[i]].join(', ')}`;
}
}
Copy link
Member

Choose a reason for hiding this comment

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

Let's improve readability here.

Use Array#filter to skip properties where their names and/or values to do not meet criteria.

Use Array#map method to convert property names into property values.

if (i < props.length - 1)
^^ Not a good strategy. As a peer developer I am tasked to add 20 properties and move friends somewhere in the middle. To easy to make a mistake. In fact, you treat frends in a different way not because they are last property but because they are Array.isArray(variable) === true.

Glue them all with Array#join.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Tnx for the suggestions. I just refactored the code according to them. Please, re-review.

print(representation);
});