Skip to content

Commit

Permalink
add my ugly solution. i think so, maybe not))
Browse files Browse the repository at this point in the history
  • Loading branch information
GavriliukArtem committed Aug 8, 2023
1 parent 8de408a commit d7c453c
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 3 deletions.
2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ Implement a function accepting 2 arguments: `state` and `actions`. The function
should change the `state` basing on the given `actions` array.

- `state` is an object. You are supposed to add, change, or delete its
properties instead of creating a new object
properties вместоd of creating a new object

- `actions` is an array of objects. Each object in this array has the next properties:
- `type` contains a string: either `'addProperties'`, `'removeProperties'` or `'clear'`;
Expand Down
32 changes: 30 additions & 2 deletions src/transformState.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,36 @@
* @param {Object} state
* @param {Object[]} actions
*/
function transformState(state, actions) {
// write code here
function transformState(state, action) {
for (const obj of action) {
// to iterate all objects in array.
for (const [key, value] of Object.entries(obj)) {
// to iterate every object and split on keys and values.
//
// conditions check
if (obj[key] === obj.type && value === 'clear') {
for (const ke of Object.keys(state)) {
delete state[ke];
}
}

if (obj[key] === obj.extraData) {
// add key and value to the object
for (const [k, v] of Object.entries(obj[key])) {
state[k] = v;
}
}

if (obj[key] === obj.keysToRemove) {
// delete properies
for (const k of obj[key]) {
delete state[k];
}
}
}
}

return state;
}

module.exports = transformState;

0 comments on commit d7c453c

Please sign in to comment.