2.1 KiB
id | title | challengeType | forumTopicId | dashedName |
---|---|---|---|---|
587d7da9367417b2b2512b67 | Aggiungere elementi alla fine di un array usando concat invece di push | 1 | 301226 | add-elements-to-the-end-of-an-array-using-concat-instead-of-push |
--description--
La programmazione funzionale riguarda la creazione e l'utilizzo di funzioni non mutanti.
L'ultima sfida ha introdotto il metodo concat
come modo per unire degli array in un nuovo array senza mutare gli array originali. Confronta concat
con il metodo push
. push
aggiunge degli elementi alla fine dello stesso array sul quale viene chiamato, mutando quello stesso array. Ecco un esempio:
const arr = [1, 2, 3];
arr.push(4, 5, 6);
arr
avrà un valore modificato di [1, 2, 3, 4, 5, 6]
, che non è in linea con la programmazione funzionale.
concat
offre un modo per unire nuovi elementi alla fine di un array senza effetti collaterali di mutazione.
--instructions--
Cambia la funzione nonMutatingPush
in modo che utilizzi concat
per aggiungere newItem
alla fine di original
senza mutare gli array original
o newItem
. La funzione dovrebbe restituire un array.
--hints--
Dovresti usare il metodo concat
.
assert(code.match(/\.concat/g));
Non dovresti usare il metodo push
.
assert(!code.match(/\.?[\s\S]*?push/g));
L'array first
non dovrebbe cambiare.
assert(JSON.stringify(first) === JSON.stringify([1, 2, 3]));
L'array second
non dovrebbe cambiare.
assert(JSON.stringify(second) === JSON.stringify([4, 5]));
nonMutatingPush([1, 2, 3], [4, 5])
dovrebbe restituire [1, 2, 3, 4, 5]
.
assert(
JSON.stringify(nonMutatingPush([1, 2, 3], [4, 5])) ===
JSON.stringify([1, 2, 3, 4, 5])
);
--seed--
--seed-contents--
function nonMutatingPush(original, newItem) {
// Only change code below this line
return original.push(newItem);
// Only change code above this line
}
const first = [1, 2, 3];
const second = [4, 5];
nonMutatingPush(first, second);
--solutions--
function nonMutatingPush(original, newItem) {
return original.concat(newItem);
}
const first = [1, 2, 3];
const second = [4, 5];