-
Notifications
You must be signed in to change notification settings - Fork 2.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
solution #2737
base: master
Are you sure you want to change the base?
solution #2737
Changes from 5 commits
c3404b7
9134591
8ad514f
d76f5a8
3aa537e
5249124
6d026f9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,30 @@ | ||
'use strict'; | ||
"use strict"; | ||
|
||
/** | ||
* @param {Object} state | ||
* @param {Object[]} actions | ||
*/ | ||
function transformState(state, actions) { | ||
// write code here | ||
for (const action of actions) { | ||
const { type, extraData, keysToRemove } = action; | ||
|
||
switch (type) { | ||
case "addProperties": | ||
Object.assign(state, extraData); | ||
break; | ||
|
||
case "removeProperties": | ||
for (const key of keysToRemove) { | ||
delete state[key]; | ||
} | ||
break; | ||
|
||
case "clear": | ||
for (const key in state) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. However, there is a small issue with the "clear" case. When you want to clear all properties from the state object, using a for...in loop may not work as expected. Instead, you should use Object.keys to get an array of keys and then delete each key. |
||
delete state[key]; | ||
} | ||
break; | ||
|
||
default: | ||
return "Error: Type Unknown"; | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In the 'default' case of your switch statement, you are returning a string. This will end the execution of the function, even if there are more valid actions in the 'actions' array. Instead, consider logging the error or ignoring the action with an invalid type. |
||
} | ||
} | ||
|
||
module.exports = transformState; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The destructuring of 'action' assumes that every action object has 'extraData' and 'keysToRemove' properties. This can potentially cause 'undefined' errors if these properties are absent. Consider destructuring these properties inside their respective case blocks.