Skip to content

Commit

Permalink
Solution
Browse files Browse the repository at this point in the history
  • Loading branch information
ForDeOne committed Dec 26, 2024
1 parent 7aa3e8a commit 956ee6e
Showing 1 changed file with 40 additions and 1 deletion.
41 changes: 40 additions & 1 deletion src/transformStateWithClones.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;

0 comments on commit 956ee6e

Please sign in to comment.