-
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
add task solution #4130
base: master
Are you sure you want to change the base?
add task solution #4130
Changes from all commits
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 |
---|---|---|
|
@@ -8,6 +8,24 @@ | |
*/ | ||
function transformStateWithClones(state, actions) { | ||
// write code here | ||
let stateHistory = { ...state }; | ||
const states = []; | ||
|
||
for (const action of actions) { | ||
if (action.type === 'addProperties') { | ||
Object.assign(stateHistory, action.extraData); | ||
} else if (action.type === 'removeProperties') { | ||
for (const key of action.keysToRemove) { | ||
delete stateHistory[key]; | ||
} | ||
} else if (action.type === 'clear') { | ||
stateHistory = {}; | ||
} | ||
Comment on lines
+15
to
+23
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 task suggests using a |
||
|
||
states.push({ ...stateHistory }); | ||
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 |
||
} | ||
|
||
return states; | ||
} | ||
|
||
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
stateHistory
object is being mutated directly within the loop. According to the task requirements, you should create a new copy of the object for each action to avoid unintended side effects. Consider making a copy ofstateHistory
at the beginning of each loop iteration.