1.9 KiB
1.9 KiB
title, id, challengeType
title | id | challengeType |
---|---|---|
Deepcopy | 596a8888ab7c01048de257d5 | 5 |
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:
Objects with properties that are functions Date objects or object with properties that are Date objects RegEx or object with properties that are RegEx objects Prototype copyingInstructions
Tests
- text: <code>deepcopy</code> should be a function.
testString: 'assert(typeof deepcopy === ''function'', ''<code>deepcopy</code> should be a function.'');'
- text: '<code>deepcopy({test: "test"})</code> should return an object.'
testString: 'assert(typeof deepcopy(obj1) === ''object'', ''<code>deepcopy({test: "test"})</code> should return an object.'');'
- text: Should not return the same object that was provided.
testString: 'assert(deepcopy(obj2) != obj2, ''Should not return the same object that was provided.'');'
- text: 'When passed an object containing an array, should return a deep copy of the object.'
testString: 'assert.deepEqual(deepcopy(obj2), obj2, ''When passed an object containing an array, should return a deep copy of the object.'');'
- text: 'When passed an object containing another object, should return a deep copy of the object.'
testString: 'assert.deepEqual(deepcopy(obj3), obj3, ''When passed an object containing another object, should return a deep copy of the object.'');'
Challenge Seed
function deepcopy (obj) {
// Good luck!
return true;
}
After Test
console.info('after the test');
Solution
function deepcopy(obj) {
return JSON.parse(JSON.stringify(obj));
}