2.6 KiB
Raw Blame History

id, title, challengeType, forumTopicId, localeTitle
id title challengeType forumTopicId localeTitle
587d7da9367417b2b2512b66 Combine Two Arrays Using the concat Method 1 301229 Объединить два массива с помощью метода concat

Description

Concatenation означает объединение объектов до конца. JavaScript предлагает метод concat для строк и массивов, которые работают одинаково. Для массивов метод вызывается на один, затем другой массив предоставляется как аргумент concat , который добавляется в конец первого массива. Он возвращает новый массив и не мутирует ни один из исходных массивов. Вот пример:
[1, 2, 3] .concat ([4, 5, 6]);
// Возвращает новый массив [1, 2, 3, 4, 5, 6]

Instructions

Используйте concat метод в nonMutatingConcat функции конкатенации attach к концу original . Функция должна возвращать конкатенированный массив.

Tests

tests:
  - text: Your code should use the <code>concat</code> method.
    testString: assert(code.match(/\.concat/g));
  - text: The <code>first</code> array should not change.
    testString: assert(JSON.stringify(first) === JSON.stringify([1, 2, 3]));
  - text: The <code>second</code> array should not change.
    testString: assert(JSON.stringify(second) === JSON.stringify([4, 5]));
  - text: <code>nonMutatingConcat([1, 2, 3], [4, 5])</code> should return <code>[1, 2, 3, 4, 5]</code>.
    testString: assert(JSON.stringify(nonMutatingConcat([1, 2, 3], [4, 5])) === JSON.stringify([1, 2, 3, 4, 5]));

Challenge Seed

function nonMutatingConcat(original, attach) {
  // Add your code below this line


  // Add your code above this line
}
var first = [1, 2, 3];
var second = [4, 5];
nonMutatingConcat(first, second);

Solution

function nonMutatingConcat(original, attach) {
  // Add your code below this line
  return original.concat(attach);
  // Add your code above this line
}
var first = [1, 2, 3];
var second = [4, 5];
nonMutatingConcat(first, second);