Skip to content
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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 25 additions & 1 deletion src/transformStateWithClones.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 });

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.

}

if (action.type === 'removeProperties') {
for (const key of action.keysToRemove) {
delete newState[key];
}
stateHistory.push({ ...newState });

Choose a reason for hiding this comment

The 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 statements to ensure the state is recorded once per action.

}

if (action.type === 'clear') {
for (const key in newState) {
delete newState[key];
}
stateHistory.push({ ...newState });

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line should also be moved outside of the if statements to ensure the state is recorded once per action.

}
}

return stateHistory;
}

module.exports = transformStateWithClones;