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

A Tiny JS World #220

Merged
merged 5 commits into from
Aug 22, 2022
Merged

Conversation

SofiiaTrokhymchuk
Copy link
Contributor

A Tiny JS World

Demo |
Code base

The code is submitted in a dedicated feature branch.

Only code files are submitted.

Please, review.

Copy link
Member

@OleksiyRudenko OleksiyRudenko left a comment

Choose a reason for hiding this comment

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

@SofiiaTrokhymchuk well done!
A couple of improvements and we're good to go.

Comment on lines 53 to 58
const friends = [];
if(inhabitant.friends === undefined){
friends.push(0);
}else{
friends.push(...inhabitant.friends);
}
Copy link
Member

Choose a reason for hiding this comment

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

  1. Can we employ ternary operator here?
  2. friends.push(0): why not assign a value of [0]?
  3. friends.push(...inhabitant.friends): arr = [...sourceArr] is another (ES6) way to copy elements into a new array. But why do we need to have a copy at all?

@OleksiyRudenko OleksiyRudenko self-assigned this Aug 21, 2022
Copy link
Member

@OleksiyRudenko OleksiyRudenko left a comment

Choose a reason for hiding this comment

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

@SofiiaTrokhymchuk a good optimization step.
Let's make yet another one to have even less verbose code.

Comment on lines 53 to 54
let friends = [];
inhabitant.friends === undefined ? friends[0] = 0 : friends = inhabitant.friends;
Copy link
Member

Choose a reason for hiding this comment

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

  1. Proper use of Ternary operator
  2. [0] is a legitimate assignable value.
  3. These 2 statements can be a single statements.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

You mean, that I need to combine a declaration and initialisation of friends and ternary operator into a single statement?

Copy link
Member

Choose a reason for hiding this comment

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

Yes

Copy link
Member

@OleksiyRudenko OleksiyRudenko left a comment

Choose a reason for hiding this comment

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

@SofiiaTrokhymchuk nice try. Although it went sideways.

@@ -50,10 +50,9 @@ catWoman.legs = 2;
catWoman.hands = 2;

function inhabitantToString(inhabitant){
let friends = [];
inhabitant.friends === undefined ? friends[0] = 0 : friends = inhabitant.friends;
inhabitant.friends === undefined ? inhabitant.friends = [0] : inhabitant.friends = inhabitant.friends;
Copy link
Member

Choose a reason for hiding this comment

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

The intention is good. However, there two issues:

  1. You mutate original property value. Other fragments of project may want different interpretation of the situation when friends are not defined.
  2. That's exactly an antipattern (condition ? targetVariable = x : targetVariable = y) for the ternary operator.

Please bring back interim friends variable and retry. Dedicate more time to the linked above article. You will use ternaries often, it is worth mastering.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sorry, I didn't pay my attention enough. 5 minutes and it will be fixed!

Copy link
Member

@OleksiyRudenko OleksiyRudenko left a comment

Choose a reason for hiding this comment

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

@SofiiaTrokhymchuk great job!

@OleksiyRudenko OleksiyRudenko merged commit 8153b6a into kottans:main Aug 22, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants