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

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

let currentState = { ...state }; // Create an initial copy of the state

for (const action of actions) {
switch (action.type) {
case 'addProperties':
currentState = addProperties(currentState, action.extraData);
break;

case 'removeProperties':
currentState = removeProperties(currentState, action.keysToRemove);
break;

case 'clear':
currentState = clearProperties(currentState);
break;
}
Comment on lines +15 to +27

Choose a reason for hiding this comment

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

Consider adding a default case in the switch statement to handle unexpected action types. This will improve error handling and make the code more robust.


stateHistory.push(currentState);

Choose a reason for hiding this comment

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

The current implementation pushes the currentState to stateHistory inside 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. Consider moving this line outside of the switch block, just before the end of the loop.

Choose a reason for hiding this comment

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

The current implementation pushes currentState to stateHistory inside the loop but within the switch block. According to the task requirements, 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 stateHistory;
}

function addProperties(state, extraData) {
return { ...state, ...extraData }; // Create a new object with added propertie
}

function removeProperties(state, keysToRemove) {
const newState = { ...state }; // Clone the state

for (const key of keysToRemove) {
delete newState[key]; // Remove the specified keys
}

return newState;
}

function clearProperties(state) {
return {}; // Return a new empty object
}

module.exports = transformStateWithClones;
Loading