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 #4130

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
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
18 changes: 18 additions & 0 deletions src/transformStateWithClones.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,24 @@
*/
function transformStateWithClones(state, actions) {
// write code here
let stateHistory = { ...state };

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 of stateHistory at the beginning of each loop iteration.

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

Choose a reason for hiding this comment

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

The task suggests using a switch statement with a default case for better readability and error handling, instead of if-else statements. Consider refactoring this part of the code to use a switch statement.


states.push({ ...stateHistory });

Choose a reason for hiding this comment

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

The states.push({ ...stateHistory }); line should be placed outside of the if-else block, at the end of each loop cycle. This ensures that the state is pushed after processing the current action, as per the additional prompt instructions.

}

return states;
}

module.exports = transformStateWithClones;
Loading