From 1fb9bc5ac0f8725b783f5f933fbcd247d4162eff Mon Sep 17 00:00:00 2001 From: Patryk Date: Mon, 14 Aug 2023 14:39:28 +0200 Subject: [PATCH 1/3] Solution --- src/transformState.js | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/src/transformState.js b/src/transformState.js index a1eaa0640..6e426eb41 100644 --- a/src/transformState.js +++ b/src/transformState.js @@ -5,7 +5,26 @@ * @param {Object[]} actions */ function transformState(state, actions) { - // write code here -} + for (const action of actions) { + if (action.type === 'addProperties') { + const extraData = action.extraData; + + for (const key in extraData) { + state[key] = extraData[key]; + } + } else if (action.type === 'removeProperties') { + const keysToRemove = action.keysToRemove; + for (const key of keysToRemove) { + if (state.hasOwnProperty(key)) { + delete state[key]; + } + } + } else if (action.type === 'clear') { + for (const key in state) { + delete state[key]; + } + } + } +} module.exports = transformState; From ef96248ad1d2e9db1a5219df4e32eedac669e0c6 Mon Sep 17 00:00:00 2001 From: Patrykmclaren14 Date: Mon, 30 Oct 2023 13:03:20 +0100 Subject: [PATCH 2/3] add task solution --- src/transformState.js | 41 +++++++++++++++++++++++++++-------------- 1 file changed, 27 insertions(+), 14 deletions(-) diff --git a/src/transformState.js b/src/transformState.js index 6e426eb41..27fefc8c9 100644 --- a/src/transformState.js +++ b/src/transformState.js @@ -4,27 +4,40 @@ * @param {Object} state * @param {Object[]} actions */ + +const add = 'addProperties'; +const remove = 'removeProperties'; +const clear = 'clear'; + function transformState(state, actions) { for (const action of actions) { - if (action.type === 'addProperties') { - const extraData = action.extraData; + switch (action.type) { + case add: + for (const key in action.extraData) { + state[key] = action.extraData[key]; + } + break; - for (const key in extraData) { - state[key] = extraData[key]; - } - } else if (action.type === 'removeProperties') { - const keysToRemove = action.keysToRemove; + case remove: + for (const key of action.keysToRemove) { + if (state.hasOwnProperty(key)) { + delete state[key]; + } + } + break; - for (const key of keysToRemove) { - if (state.hasOwnProperty(key)) { + case clear: + for (const key in state) { delete state[key]; } - } - } else if (action.type === 'clear') { - for (const key in state) { - delete state[key]; - } + break; + + default: + return 'Unknown action type'; } } + + return state; } + module.exports = transformState; From ed94764016722438e29386e7347c1bb8e3bc585d Mon Sep 17 00:00:00 2001 From: Patrykmclaren14 Date: Tue, 31 Oct 2023 12:03:37 +0100 Subject: [PATCH 3/3] solution --- src/transformState.js | 24 +++++++++++------------- 1 file changed, 11 insertions(+), 13 deletions(-) diff --git a/src/transformState.js b/src/transformState.js index 27fefc8c9..069700da9 100644 --- a/src/transformState.js +++ b/src/transformState.js @@ -5,31 +5,29 @@ * @param {Object[]} actions */ -const add = 'addProperties'; -const remove = 'removeProperties'; -const clear = 'clear'; +const ADD = 'addProperties'; +const REMOVE = 'removeProperties'; +const CLEAR = 'clear'; function transformState(state, actions) { for (const action of actions) { switch (action.type) { - case add: - for (const key in action.extraData) { - state[key] = action.extraData[key]; - } + case ADD: + Object.assign(state, action.extraData); break; - case remove: - for (const key of action.keysToRemove) { + case REMOVE: + action.keysToRemove.forEach(key => { if (state.hasOwnProperty(key)) { delete state[key]; } - } + }); break; - case clear: - for (const key in state) { + case CLEAR: + Object.keys(state).forEach(key => { delete state[key]; - } + }); break; default: