-
-
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
A Tiny JS World #220
A Tiny JS World #220
Conversation
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.
@SofiiaTrokhymchuk well done!
A couple of improvements and we're good to go.
const friends = []; | ||
if(inhabitant.friends === undefined){ | ||
friends.push(0); | ||
}else{ | ||
friends.push(...inhabitant.friends); | ||
} |
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.
- Can we employ ternary operator here?
friends.push(0)
: why not assign a value of[0]
?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?
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.
@SofiiaTrokhymchuk a good optimization step.
Let's make yet another one to have even less verbose code.
let friends = []; | ||
inhabitant.friends === undefined ? friends[0] = 0 : friends = inhabitant.friends; |
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.
- Proper use of Ternary operator
[0]
is a legitimate assignable value.- These 2 statements can be a single statements.
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 mean, that I need to combine a declaration and initialisation of friends
and ternary operator into a single statement?
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.
Yes
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.
@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; |
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.
The intention is good. However, there two issues:
- You mutate original property value. Other fragments of project may want different interpretation of the situation when friends are not defined.
- 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.
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.
Sorry, I didn't pay my attention enough. 5 minutes and it will be fixed!
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.
@SofiiaTrokhymchuk great job!
A Tiny JS World
Demo |
Code base
The code is submitted in a dedicated feature branch.
Only code files are submitted.
Please, review.