Skip to content

Commit

Permalink
Solution
Browse files Browse the repository at this point in the history
  • Loading branch information
dimon committed Dec 26, 2024
1 parent 7aa3e8a commit 073d4b9
Showing 1 changed file with 42 additions and 1 deletion.
43 changes: 42 additions & 1 deletion src/transformStateWithClones.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,48 @@
* @return {Object[]}
*/
function transformStateWithClones(state, actions) {
// write code here
const result = [];
let currentState = { ...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 = clear(currentState);

break;
}
result.push(currentState);
}

return result;
}

function addProperties(state, extraData) {
return { ...state, ...extraData };
}

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

for (const key of keysToRemove) {
delete newState[key];
}

return newState;
}

function clear(state) {
return {};
}

module.exports = transformStateWithClones;

0 comments on commit 073d4b9

Please sign in to comment.