freeCodeCamp/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/functional-programming/add-elements-to-the-end-of-an-array-using-concat-instead-of-push.chinese.md
Kristofer Koishigawa b3213fc892 fix(i18n): chinese test suite (#38220)
* fix: Chinese test suite

Add localeTiltes, descriptions, and adjust test text and testStrings to get the automated test suite working.

* fix: ran script, updated testStrings and solutions
2020-03-03 18:49:47 +05:30

2.2 KiB
Raw Blame History

id, title, challengeType, videoUrl, localeTitle
id title challengeType videoUrl localeTitle
587d7da9367417b2b2512b67 Add Elements to the End of an Array Using concat Instead of push 1 使用concat将元素添加到数组的末尾而不是push

Description

函数式编程就是创建和使用非变异函数。最后一个挑战是将concat方法作为一种将数组组合成新数组而不改变原始数组的方法。将concatpush方法进行比较。 Push将一个项添加到调用它的同一个数组的末尾,这会改变该数组。这是一个例子:
var arr = [1,2,3];
arr.push[4,5,6];
// arr更改为[1,2,3[4,5,6]]
//不是函数式编程方式
Concat提供了一种在数组末尾添加新项目而无任何变异副作用的方法。

Instructions

更改nonMutatingPush函数,使其使用concatnewItem添加到original结尾而不是push 。该函数应返回一个数组。

Tests

tests:
  - text: 您的代码应使用<code>concat</code>方法。
    testString: assert(code.match(/\.concat/g));
  - text: 您的代码不应使用<code>push</code>方法。
    testString: assert(!code.match(/\.push/g));
  - text: 第<code>first</code>数组不应该改变。
    testString: assert(JSON.stringify(first) === JSON.stringify([1, 2, 3]));
  - text: <code>second</code>数组不应该改变。
    testString: assert(JSON.stringify(second) === JSON.stringify([4, 5]));
  - text: '<code>nonMutatingPush([1, 2, 3], [4, 5])</code>应该返回<code>[1, 2, 3, 4, 5]</code> 。'
    testString: assert(JSON.stringify(nonMutatingPush([1, 2, 3], [4, 5])) === JSON.stringify([1, 2, 3, 4, 5]));

Challenge Seed

function nonMutatingPush(original, newItem) {
  // Add your code below this line
  return original.push(newItem);

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

Solution

// solution required