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

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

for (const action of actions) {
switch (action.type) {
case 'clear':
currentState = {};
break;
case 'addProperties':
currentState = {
...currentState,
...action.extraData,
};
break;
case 'removeProperties':
currentState = Object.keys(currentState)
.filter((key) => !action.keysToRemove.includes(key))
.reduce((newState, key) => {
newState[key] = currentState[key];

return newState;
}, {});
break;
default:
break;
}
states.push({ ...currentState });
Comment on lines +35 to +36

Choose a reason for hiding this comment

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

The states.push({ ...currentState }); operation should be moved outside of the switch block. According to the additional prompt rules, 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.

}

return states;
}

module.exports = transformStateWithClones;
Loading