mrugesh 22afc2a0ca feat(learn): python certification projects (#38216)
Co-authored-by: Oliver Eyton-Williams <ojeytonwilliams@gmail.com>
Co-authored-by: Kristofer Koishigawa <scissorsneedfoodtoo@gmail.com>
Co-authored-by: Beau Carnes <beaucarnes@gmail.com>
2020-05-27 13:19:08 +05:30

1.8 KiB

title, id, challengeType, isHidden, forumTopicId
title id challengeType isHidden forumTopicId
Deepcopy 596a8888ab7c01048de257d5 5 false 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:
  • 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 copying

Instructions

Tests

tests:
  - text: <code>deepcopy</code> should be a function.
    testString: assert(typeof deepcopy === 'function');
  - text: '<code>deepcopy({test: "test"})</code> should return an object.'
    testString: 'assert(typeof deepcopy(obj1) === ''object'');'
  - text: <code>deepcopy</code> should not return the same object that was provided.
    testString: assert(deepcopy(obj2) != obj2);
  - text: When passed an object containing an array, <code>deepcopy</code> should return a deep copy of the object.
    testString: assert.deepEqual(deepcopy(obj2), obj2);
  - text: When passed an object containing another object, <code>deepcopy</code>  should return a deep copy of the object.
    testString: assert.deepEqual(deepcopy(obj3), obj3);

Challenge Seed

function deepcopy(obj) {
  // Good luck!
  return true;
}

After Test

const obj1 = { test: 'test' };
const obj2 = {
  t: 'test',
  a: ['an', 'array']
};
const obj3 = {
  t: 'try',
  o: obj2
};

Solution

function deepcopy(obj) {
  return JSON.parse(JSON.stringify(obj));
}