freeCodeCamp/curriculum/challenges/chinese/02-javascript-algorithms-an.../functional-programming/combine-two-arrays-using-th...

2.2 KiB
Raw Blame History

id title challengeType videoUrl localeTitle
587d7da9367417b2b2512b66 Combine Two Arrays Using the concat Method 1 使用concat方法组合两个数组

Description

Concatenation意味着端到端地连接项目。 JavaScript为字符串和数组提供了以相同方式工作的concat方法。对于数组,该方法在一个上调用,然后另一个数组作为concat的参数提供,该数组被添加到第一个数组的末尾。它返回一个新数组,不会改变任何一个原始数组。这是一个例子:
[1,2,3] .concat[4,5,6];
//返回一个新数组[1,2,3,4,5,6]

Instructions

使用nonMutatingConcat函数中的concat方法attachoriginal的结尾。该函数应返回连接数组。

Tests

tests:
  - text: 您的代码应使用<code>concat</code>方法。
    testString: 'assert(code.match(/\.concat/g), "Your code should use the <code>concat</code> method.");'
  - text: 第<code>first</code>数组不应该改变。
    testString: 'assert(JSON.stringify(first) === JSON.stringify([1, 2, 3]), "The <code>first</code> array should not change.");'
  - text: <code>second</code>数组不应该改变。
    testString: 'assert(JSON.stringify(second) === JSON.stringify([4, 5]), "The <code>second</code> array should not change.");'
  - text: '<code>nonMutatingConcat([1, 2, 3], [4, 5])</code>应该返回<code>[1, 2, 3, 4, 5]</code> 。'
    testString: 'assert(JSON.stringify(nonMutatingConcat([1, 2, 3], [4, 5])) === JSON.stringify([1, 2, 3, 4, 5]), "<code>nonMutatingConcat([1, 2, 3], [4, 5])</code> should return <code>[1, 2, 3, 4, 5]</code>.");'

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

// solution required