Skip to content

Commit 6b72fea

Browse files
authored
Update objects-challenges.md
1 parent 34ae88f commit 6b72fea

File tree

1 file changed

+10
-11
lines changed

1 file changed

+10
-11
lines changed

challenges/objects-challenges.md

+10-11
Original file line numberDiff line numberDiff line change
@@ -79,34 +79,33 @@ Object.setPrototypeOf(newObj, obj);
7979
- Deep copy is done by copying all the properties of the object to another object
8080

8181
```js
82-
const x = Object.assign({}, obj);
82+
Object.assign({}, obj);
8383
```
8484

8585
```js
86-
const x = { ...obj};
86+
{ ...obj};
8787
```
8888

8989
```js
90-
const x = JSON.parse(JSON.stringify(obj));
90+
JSON.parse(JSON.stringify(obj));
9191
```
9292

9393
```js
9494
function deepCopy(obj){
95-
if(!obj) return obj;
95+
if(!obj)
96+
return obj;
97+
9698
const copyObj = {};
9799
for(const key in obj){
98-
let val;
99-
if(typeof obj[key] !== 'object')
100-
val = obj[key];
100+
if(typeof obj[key] !== 'object' || Array.isArray(obj[key]))
101+
copyObj[key] = obj[key];
101102
else
102-
val = deepCopy(obj[key]);
103-
104-
copyObj[key] = val;
103+
copyObj[key] = deepCopy(obj[key]);
105104
}
106105
return copyObj;
107106
}
108107

109-
const x = deepCopy(obj);
108+
deepCopy(obj);
110109
```
111110

112111
###### Notes

0 commit comments

Comments
 (0)