-
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
solution #4112
base: master
Are you sure you want to change the base?
solution #4112
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,6 +8,40 @@ | |
*/ | ||
function transformStateWithClones(state, actions) { | ||
// write code here | ||
const array = []; | ||
const stateCopy = { ...state }; | ||
|
||
for (const action of actions) { | ||
transform(stateCopy, action); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The |
||
} | ||
|
||
function transform(result, action) { | ||
switch (action.type) { | ||
case 'addProperties': | ||
Object.assign(result, action.extraData); | ||
array.push({ ...stateCopy }); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Pushing the state copy to the array should be done after processing each action, outside of the |
||
break; | ||
|
||
case 'removeProperties': { | ||
for (const word of action.keysToRemove) { | ||
delete stateCopy[word]; | ||
} | ||
|
||
array.push({ ...stateCopy }); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Similar to the previous comment, the state copy should be pushed to the array after processing the action, outside of the |
||
break; | ||
} | ||
|
||
case 'clear': { | ||
for (const key of Object.keys(stateCopy)) { | ||
delete stateCopy[key]; | ||
} | ||
|
||
array.push({ ...stateCopy }); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ensure that the state copy is pushed to the array after processing the action, outside of the |
||
} | ||
} | ||
} | ||
|
||
return array; | ||
} | ||
|
||
module.exports = transformStateWithClones; |
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
stateCopy
should be created inside the loop, before processing each action. This ensures that each action is applied to a fresh copy of the state, maintaining the integrity of the state for each action.