-
Notifications
You must be signed in to change notification settings - Fork 3.9k
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
Task solution #2827
base: master
Are you sure you want to change the base?
Task solution #2827
Conversation
@@ -7,7 +7,43 @@ | |||
* @return {Object[]} | |||
*/ | |||
function transformStateWithClones(state, actions) { | |||
// write code here | |||
const statesArray = []; |
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.
const statesArray = []; | |
const statesArray = []; | |
const stateCopy = { ...state }; |
src/transformStateWithClones.js
Outdated
// write code here | ||
const statesArray = []; | ||
|
||
for (let i = 0; i < actions.length; i++) { |
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 can just use for of
for (let i = 0; i < actions.length; i++) { | |
for (const action of actions)) { |
src/transformStateWithClones.js
Outdated
const statesArray = []; | ||
|
||
for (let i = 0; i < actions.length; i++) { | ||
switch (actions[i].type) { |
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.
switch (actions[i].type) { | |
switch (action.type) { |
src/transformStateWithClones.js
Outdated
const currentState = i ? { ...statesArray[i - 1] } : { ...state }; | ||
|
||
Object.assign(currentState, actions[i].extraData); | ||
statesArray.push(currentState); | ||
break; |
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.
- no needs to update state at each case
- no needs to push updates at each case
- apply similar solution to cases below
const currentState = i ? { ...statesArray[i - 1] } : { ...state }; | |
Object.assign(currentState, actions[i].extraData); | |
statesArray.push(currentState); | |
break; | |
Object.assign(stateCopy, action.extraData); | |
break; |
} | ||
|
||
default: throw new Error('Invalid action type'); | ||
} |
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.
} | |
} | |
statesArray.push({ ...stateCopy }); |
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.
Good job 👍
Let's improve your code
src/transformStateWithClones.js
Outdated
for (const action of actions) { | ||
switch (action.type) { |
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.
Try to use a destruction for action
for (const action of actions) { | |
switch (action.type) { | |
for (const action of actions) { | |
const { type, ....... } = action; | |
switch (type) { |
src/transformStateWithClones.js
Outdated
|
||
for (const action of actions) { | ||
switch (action.type) { | ||
case 'addProperties': { |
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.
Consider will be better if you create a variables for these words and use them everywhere
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.
Well done
No description provided.