From 19237a72371ddf09c6e057967fa35928ecdf02fc Mon Sep 17 00:00:00 2001 From: M DD Date: Tue, 8 Aug 2023 22:21:22 +0300 Subject: [PATCH 1/2] js_stateful-object solution v1 --- src/transformState.js | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/src/transformState.js b/src/transformState.js index a1eaa0640..e75c7e325 100644 --- a/src/transformState.js +++ b/src/transformState.js @@ -5,7 +5,29 @@ * @param {Object[]} actions */ function transformState(state, actions) { - // write code here + for (const action of actions) { + const actionType = action.type; + const actionExtraData = action.extraData; + const actionKeysToRemove = action.keysToRemove; + + if (actionType === 'addProperties') { + Object.assign(state, actionExtraData); + } + + if (actionType === 'removeProperties') { + for (const property of actionKeysToRemove) { + delete state[property]; + } + } + + if (actionType === 'clear') { + for (const element in state) { + delete state[element]; + } + } + } + + return state; } module.exports = transformState; From 05c6bd669275732c1720138a40264462c6c65cf2 Mon Sep 17 00:00:00 2001 From: M DD Date: Fri, 11 Aug 2023 15:45:12 +0300 Subject: [PATCH 2/2] js_stateful-object solution v2 --- src/transformState.js | 31 +++++++++++++++++-------------- 1 file changed, 17 insertions(+), 14 deletions(-) diff --git a/src/transformState.js b/src/transformState.js index e75c7e325..28f75ed47 100644 --- a/src/transformState.js +++ b/src/transformState.js @@ -7,23 +7,26 @@ function transformState(state, actions) { for (const action of actions) { const actionType = action.type; - const actionExtraData = action.extraData; - const actionKeysToRemove = action.keysToRemove; - if (actionType === 'addProperties') { - Object.assign(state, actionExtraData); - } + switch (actionType) { + case 'addProperties': + Object.assign(state, action.extraData); + break; - if (actionType === 'removeProperties') { - for (const property of actionKeysToRemove) { - delete state[property]; - } - } + case 'removeProperties': + for (const property of action.keysToRemove) { + delete state[property]; + } + break; + + case 'clear': + for (const element in state) { + delete state[element]; + } + break; - if (actionType === 'clear') { - for (const element in state) { - delete state[element]; - } + default: + break; } }