-
Notifications
You must be signed in to change notification settings - Fork 4k
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
add task solution #4228
base: master
Are you sure you want to change the base?
add task solution #4228
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,7 +7,31 @@ | |
* @return {Object[]} | ||
*/ | ||
function transformStateWithClones(state, actions) { | ||
// write code here | ||
const newState = { ...state }; | ||
const stateHistory = []; | ||
|
||
for (const action of actions) { | ||
if (action.type === 'addProperties') { | ||
Object.assign(newState, action.extraData); | ||
stateHistory.push({ ...newState }); | ||
} | ||
|
||
if (action.type === 'removeProperties') { | ||
for (const key of action.keysToRemove) { | ||
delete newState[key]; | ||
} | ||
stateHistory.push({ ...newState }); | ||
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, this line should be moved outside of the |
||
} | ||
|
||
if (action.type === 'clear') { | ||
for (const key in newState) { | ||
delete newState[key]; | ||
} | ||
stateHistory.push({ ...newState }); | ||
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. This line should also be moved outside of the |
||
} | ||
} | ||
|
||
return stateHistory; | ||
} | ||
|
||
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.
Instead of pushing the new state inside each action type block, consider moving this line outside of the
if
statements to ensure the state is recorded once per action. This aligns with the guidance to push a copy of the object at the end of each loop cycle.