diff --git a/src/transformStateWithClones.js b/src/transformStateWithClones.js index 96d4f7e7e..b2f9754b6 100644 --- a/src/transformStateWithClones.js +++ b/src/transformStateWithClones.js @@ -7,7 +7,46 @@ * @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; + } + stateHistory.push(currentState); + } + + 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;