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

solution #4112

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
37 changes: 21 additions & 16 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"license": "GPL-3.0",
"devDependencies": {
"@mate-academy/eslint-config": "latest",
"@mate-academy/scripts": "^1.8.6",
"@mate-academy/scripts": "^1.9.12",
"eslint": "^8.57.0",
"eslint-plugin-jest": "^28.6.0",
"eslint-plugin-node": "^11.1.0",
Expand Down
34 changes: 34 additions & 0 deletions src/transformStateWithClones.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,40 @@
*/
function transformStateWithClones(state, actions) {
// write code here
const array = [];
const stateCopy = { ...state };

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.


for (const action of actions) {
transform(stateCopy, action);

Choose a reason for hiding this comment

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

The transform function modifies stateCopy, but since stateCopy is not re-created for each action, the changes accumulate across actions. Move the creation of stateCopy inside the loop to fix this.

}

function transform(result, action) {
switch (action.type) {
case 'addProperties':
Object.assign(result, action.extraData);
array.push({ ...stateCopy });

Choose a reason for hiding this comment

The 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 switch block. This ensures that the state is captured after all modifications for the current action are complete.

break;

case 'removeProperties': {
for (const word of action.keysToRemove) {
delete stateCopy[word];
}

array.push({ ...stateCopy });

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, the state copy should be pushed to the array after processing the action, outside of the switch block.

break;
}

case 'clear': {
for (const key of Object.keys(stateCopy)) {
delete stateCopy[key];
}

array.push({ ...stateCopy });

Choose a reason for hiding this comment

The 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 switch block.

}
}
}

return array;
}

module.exports = transformStateWithClones;
Loading