Files
freeCodeCamp/curriculum/challenges/italian/22-rosetta-code/rosetta-code-challenges/deepcopy.md
2024-01-24 19:52:36 +01:00

1.5 KiB

id, title, challengeType, forumTopicId, dashedName
id title challengeType forumTopicId dashedName
596a8888ab7c01048de257d5 Deepcopy 1 302247 deepcopy

--description--

Write a function that returns a deep copy of a given object. The copy must not be the same object that was given.

Questa sfida non testa per:

  • Objects with properties that are functions
  • Oggetti Date o oggetti con proprietà che sono oggetti Date
  • RegEx o oggetti con proprietà che sono RegEx
  • Copiamento del prototype

--hints--

deepcopy dovrebbe essere una funzione.

assert(typeof deepcopy === 'function');

deepcopy({test: "test"}) dovrebbe restituire un oggetto.

assert(typeof deepcopy(obj1) === 'object');

deepcopy non dovrebbe restituire lo stesso oggetto dato.

assert(deepcopy(obj2) != obj2);

Quando gli viene dato un oggetto contenente un array, deepcopy dovrebbe restituire una copia profonda dell'oggetto.

assert.deepEqual(deepcopy(obj2), obj2);

Quando gli viene dato un oggetto contenente un altro oggetto, deepcopy dovrebbe restituire una copia profonda dell'oggetto.

assert.deepEqual(deepcopy(obj3), obj3);

--seed--

--after-user-code--

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

--seed-contents--

function deepcopy(obj) {

  return true;
}

--solutions--

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