From 93c8b6c9894764de9f9852991b87c34d377a192f Mon Sep 17 00:00:00 2001 From: Your Name Date: Thu, 2 Nov 2023 14:19:43 +0200 Subject: [PATCH 1/2] Solution --- src/transformState.js | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/src/transformState.js b/src/transformState.js index a1eaa064..48f7221b 100644 --- a/src/transformState.js +++ b/src/transformState.js @@ -5,7 +5,27 @@ * @param {Object[]} actions */ function transformState(state, actions) { - // write code here + for (const action of actions) { + switch (action.type) { + case 'addProperties': + for (const data in action.extraData) { + const prop = action.extraData[data]; + + state[data] = prop; + } + break; + case 'removeProperties': + for (const key of action.keysToRemove) { + delete state[key]; + } + break; + case 'clear': + for (const key in state) { + delete state[key]; + } + break; + } + } } module.exports = transformState; From 3dc872868346b74308f80b4ccd0c9cfda4225fba Mon Sep 17 00:00:00 2001 From: Your Name Date: Thu, 2 Nov 2023 21:09:51 +0200 Subject: [PATCH 2/2] Solution1 --- src/transformState.js | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/src/transformState.js b/src/transformState.js index 48f7221b..9d7b6152 100644 --- a/src/transformState.js +++ b/src/transformState.js @@ -6,22 +6,24 @@ */ function transformState(state, actions) { for (const action of actions) { + // the function changes the given object `state` according to the + // specified conditions in `actions` array of objects switch (action.type) { case 'addProperties': - for (const data in action.extraData) { - const prop = action.extraData[data]; + for (const property in action.extraData) { + const value = action.extraData[property]; - state[data] = prop; + state[property] = value; } break; case 'removeProperties': - for (const key of action.keysToRemove) { - delete state[key]; + for (const propertyName of action.keysToRemove) { + delete state[propertyName]; } break; case 'clear': - for (const key in state) { - delete state[key]; + for (const propertyName in state) { + delete state[propertyName]; } break; }