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

Open
wants to merge 1 commit 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
20 changes: 19 additions & 1 deletion src/transformStateWithClones.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,25 @@
* @return {Object[]}
*/
function transformStateWithClones(state, actions) {
// write code here
let currentState = { ...state };
const states = [];

for (const action of actions) {
if (action.type === 'clear') {
currentState = {};
} else if (action.type === 'addProperties') {
currentState = { ...currentState, ...action.extraData };
} else if (action.type === 'removeProperties') {
currentState = { ...currentState };

for (const key of action.keysToRemove) {
delete currentState[key];
}
}
states.push(currentState);

Choose a reason for hiding this comment

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

The current state is being pushed into the states array inside the loop but within the conditional blocks. According to the additional prompt, it is better to push a copy of the object to the array at the end of each loop cycle, but outside of the switch block, after the current action is processed. Consider moving this line outside of the conditional blocks to ensure the state is captured after all modifications for the current action are complete.

}

return states;
}

module.exports = transformStateWithClones;