1.9 KiB
id | title | challengeType | forumTopicId | dashedName |
---|---|---|---|---|
587d7da9367417b2b2512b66 | Combinare due array usando il metodo concat | 1 | 301229 | combine-two-arrays-using-the-concat-method |
--description--
Concatenazione significa unire gli oggetti dal primo all'ultimo. JavaScript offre il metodo concat
sia per le stringhe per gli array, e funziona allo stesso modo per entrambi. Per gli array, il metodo viene chiamato su un array, quindi viene fornito un altro array come argomento a concat
, in modo da aggiungerlo alla fine del primo array. Questo restituisce un nuovo array e non muta nessuno degli array originali. Ecco un esempio:
[1, 2, 3].concat([4, 5, 6]);
L'array restituito sarà [1, 2, 3, 4, 5, 6]
.
--instructions--
Utilizza il metodo concat
nella funzione nonMutatingConcat
per concatenare attach
alla fine di original
. La funzione dovrebbe restituire l'array concatenato.
--hints--
Il tuo codice dovrebbe usare il metodo concat
.
assert(code.match(/\.concat/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]));
nonMutatingConcat([1, 2, 3], [4, 5])
dovrebbe restituire [1, 2, 3, 4, 5]
.
assert(
JSON.stringify(nonMutatingConcat([1, 2, 3], [4, 5])) ===
JSON.stringify([1, 2, 3, 4, 5])
);
--seed--
--seed-contents--
function nonMutatingConcat(original, attach) {
// Only change code below this line
// Only change code above this line
}
var first = [1, 2, 3];
var second = [4, 5];
nonMutatingConcat(first, second);
--solutions--
function nonMutatingConcat(original, attach) {
// Only change code below this line
return original.concat(attach);
// Only change code above this line
}
var first = [1, 2, 3];
var second = [4, 5];
nonMutatingConcat(first, second);