--- title: Deepcopy id: 596a8888ab7c01048de257d5 challengeType: 5 forumTopicId: 302247 --- ## Description
Write a function that returns a deep copy of a given object. The copy must not be the same object that was given. This task will not test for:
## Instructions
## Tests
```yml tests: - text: deepcopy should be a function. testString: assert(typeof deepcopy === 'function'); - text: 'deepcopy({test: "test"}) should return an object.' testString: 'assert(typeof deepcopy(obj1) === ''object'');' - text: deepcopy should not return the same object that was provided. testString: assert(deepcopy(obj2) != obj2); - text: When passed an object containing an array, deepcopy should return a deep copy of the object. testString: assert.deepEqual(deepcopy(obj2), obj2); - text: When passed an object containing another object, deepcopy should return a deep copy of the object. testString: assert.deepEqual(deepcopy(obj3), obj3); ```
## Challenge Seed
```js function deepcopy(obj) { return true; } ```
### After Test
```js const obj1 = { test: 'test' }; const obj2 = { t: 'test', a: ['an', 'array'] }; const obj3 = { t: 'try', o: obj2 }; ```
## Solution
```js function deepcopy(obj) { return JSON.parse(JSON.stringify(obj)); } ```