chore(learn): Applied MDX format to Chinese curriculum files (#40462)

This commit is contained in:
Randell Dawson
2020-12-16 00:37:30 -07:00
committed by GitHub
parent 873fce02a2
commit 9ce4a02a41
1665 changed files with 58741 additions and 88042 deletions

View File

@ -1,80 +1,119 @@
---
id: a3f503de51cf954ede28891d
title: 找到对称差异
challengeType: 5
videoUrl: ''
title: 找到对称差异
---
## Description
<section id="description">创建一个带有两个或更多数组的函数,并返回所提供数组的<dfn>对称差</dfn> <code></code><code></code> )数组。给定两个集合(例如集合<code>A = {1, 2, 3}</code>并且集合<code>B = {2, 3, 4}</code> ),两个集合的数学术语“对称差异”是在任一集合中的元素集合。两组,但两者都没有( <code>A △ B = C = {1, 4}</code> )。对于你所采取的每一个额外的对称差异(比如在集合<code>D = {2, 3}</code> ),你应该得到具有两个集合中的任何一个但不是两个集合的元素的集合( <code>C △ D = {1, 4} △ {2, 3} = {1, 2, 3, 4}</code> )。结果数组必须仅包含唯一值( <em>不重复</em> )。如果卡住,请记得使用<a href="https://forum.freecodecamp.org/t/how-to-get-help-when-you-are-stuck-coding/19514" target="_blank">Read-Search-Ask</a> 。尝试配对程序。编写自己的代码。 </section>
# --description--
## Instructions
<section id="instructions">
</section>
创建一个带有两个或更多数组的函数,并返回所提供数组的<dfn>对称差</dfn> `△``⊕` )数组。给定两个集合(例如集合`A = {1, 2, 3}`并且集合`B = {2, 3, 4}` ),两个集合的数学术语“对称差异”是在任一集合中的元素集合。两组,但两者都没有( `A △ B = C = {1, 4}` )。对于你所采取的每一个额外的对称差异(比如在集合`D = {2, 3}` ),你应该得到具有两个集合中的任何一个但不是两个集合的元素的集合( `C △ D = {1, 4} △ {2, 3} = {1, 2, 3, 4}` )。结果数组必须仅包含唯一值( *不重复* )。如果卡住,请记得使用[Read-Search-Ask](https://forum.freecodecamp.org/t/how-to-get-help-when-you-are-stuck-coding/19514) 。尝试配对程序。编写自己的代码。
## Tests
<section id='tests'>
# --hints--
```yml
tests:
- text: '<code>sym([1, 2, 3], [5, 2, 1, 4])</code>应返回<code>[3, 4, 5]</code> 。'
testString: assert.sameMembers(sym([1, 2, 3], [5, 2, 1, 4]), [3, 4, 5]);
- text: '<code>sym([1, 2, 3], [5, 2, 1, 4])</code>应仅包含三个元素。'
testString: assert.equal(sym([1, 2, 3], [5, 2, 1, 4]).length, 3);
- text: '<code>sym([1, 2, 3, 3], [5, 2, 1, 4])</code>应该返回<code>[3, 4, 5]</code> 。'
testString: assert.sameMembers(sym([1, 2, 3, 3], [5, 2, 1, 4]), [3, 4, 5]);
- text: '<code>sym([1, 2, 3, 3], [5, 2, 1, 4])</code>应仅包含三个元素。'
testString: assert.equal(sym([1, 2, 3, 3], [5, 2, 1, 4]).length, 3);
- text: '<code>sym([1, 2, 3], [5, 2, 1, 4, 5])</code>应返回<code>[3, 4, 5]</code> 。'
testString: assert.sameMembers(sym([1, 2, 3], [5, 2, 1, 4, 5]), [3, 4, 5]);
- text: '<code>sym([1, 2, 3], [5, 2, 1, 4, 5])</code>应仅包含三个元素。'
testString: assert.equal(sym([1, 2, 3], [5, 2, 1, 4, 5]).length, 3);
- text: '<code>sym([1, 2, 5], [2, 3, 5], [3, 4, 5])</code>应该返回<code>[1, 4, 5]</code>'
testString: assert.sameMembers(sym([1, 2, 5], [2, 3, 5], [3, 4, 5]), [1, 4, 5]);
- text: '<code>sym([1, 2, 5], [2, 3, 5], [3, 4, 5])</code>应仅包含三个元素。'
testString: assert.equal(sym([1, 2, 5], [2, 3, 5], [3, 4, 5]).length, 3);
- text: '<code>sym([1, 1, 2, 5], [2, 2, 3, 5], [3, 4, 5, 5])</code>应该返回<code>[1, 4, 5]</code> 。'
testString: assert.sameMembers(sym([1, 1, 2, 5], [2, 2, 3, 5], [3, 4, 5, 5]), [1, 4, 5]);
- text: '<code>sym([1, 1, 2, 5], [2, 2, 3, 5], [3, 4, 5, 5])</code>应仅包含三个元素。'
testString: assert.equal(sym([1, 1, 2, 5], [2, 2, 3, 5], [3, 4, 5, 5]).length, 3);
- text: '<code>sym([3, 3, 3, 2, 5], [2, 1, 5, 7], [3, 4, 6, 6], [1, 2, 3])</code>应该返回<code>[2, 3, 4, 6, 7]</code> <code>sym([3, 3, 3, 2, 5], [2, 1, 5, 7], [3, 4, 6, 6], [1, 2, 3])</code> <code>[2, 3, 4, 6, 7]</code> 。'
testString: assert.sameMembers(sym([3, 3, 3, 2, 5], [2, 1, 5, 7], [3, 4, 6, 6], [1, 2, 3]), [2, 3, 4, 6, 7]);
- text: '<code>sym([3, 3, 3, 2, 5], [2, 1, 5, 7], [3, 4, 6, 6], [1, 2, 3])</code>应仅包含五个元素。'
testString: assert.equal(sym([3, 3, 3, 2, 5], [2, 1, 5, 7], [3, 4, 6, 6], [1, 2, 3]).length, 5);
- text: '<code>sym([3, 3, 3, 2, 5], [2, 1, 5, 7], [3, 4, 6, 6], [1, 2, 3], [5, 3, 9, 8], [1])</code>应该返回<code>[1, 2, 4, 5, 6, 7, 8, 9]</code> 。'
testString: assert.sameMembers(sym([3, 3, 3, 2, 5], [2, 1, 5, 7], [3, 4, 6, 6], [1, 2, 3], [5, 3, 9, 8], [1]), [1, 2, 4, 5, 6, 7, 8, 9]);
- text: '<code>sym([3, 3, 3, 2, 5], [2, 1, 5, 7], [3, 4, 6, 6], [1, 2, 3], [5, 3, 9, 8], [1])</code>应该只包含八个元素。'
testString: assert.equal(sym([3, 3, 3, 2, 5], [2, 1, 5, 7], [3, 4, 6, 6], [1, 2, 3], [5, 3, 9, 8], [1]).length, 8);
```
</section>
## Challenge Seed
<section id='challengeSeed'>
<div id='js-seed'>
`sym([1, 2, 3], [5, 2, 1, 4])`应返回`[3, 4, 5]`
```js
function sym(args) {
return args;
}
sym([1, 2, 3], [5, 2, 1, 4]);
assert.sameMembers(sym([1, 2, 3], [5, 2, 1, 4]), [3, 4, 5]);
```
</div>
</section>
## Solution
<section id='solution'>
`sym([1, 2, 3], [5, 2, 1, 4])`应仅包含三个元素。
```js
// solution required
assert.equal(sym([1, 2, 3], [5, 2, 1, 4]).length, 3);
```
/section>
`sym([1, 2, 3, 3], [5, 2, 1, 4])`应该返回`[3, 4, 5]`
```js
assert.sameMembers(sym([1, 2, 3, 3], [5, 2, 1, 4]), [3, 4, 5]);
```
`sym([1, 2, 3, 3], [5, 2, 1, 4])`应仅包含三个元素。
```js
assert.equal(sym([1, 2, 3, 3], [5, 2, 1, 4]).length, 3);
```
`sym([1, 2, 3], [5, 2, 1, 4, 5])`应返回`[3, 4, 5]`
```js
assert.sameMembers(sym([1, 2, 3], [5, 2, 1, 4, 5]), [3, 4, 5]);
```
`sym([1, 2, 3], [5, 2, 1, 4, 5])`应仅包含三个元素。
```js
assert.equal(sym([1, 2, 3], [5, 2, 1, 4, 5]).length, 3);
```
`sym([1, 2, 5], [2, 3, 5], [3, 4, 5])`应该返回`[1, 4, 5]`
```js
assert.sameMembers(sym([1, 2, 5], [2, 3, 5], [3, 4, 5]), [1, 4, 5]);
```
`sym([1, 2, 5], [2, 3, 5], [3, 4, 5])`应仅包含三个元素。
```js
assert.equal(sym([1, 2, 5], [2, 3, 5], [3, 4, 5]).length, 3);
```
`sym([1, 1, 2, 5], [2, 2, 3, 5], [3, 4, 5, 5])`应该返回`[1, 4, 5]`
```js
assert.sameMembers(sym([1, 1, 2, 5], [2, 2, 3, 5], [3, 4, 5, 5]), [1, 4, 5]);
```
`sym([1, 1, 2, 5], [2, 2, 3, 5], [3, 4, 5, 5])`应仅包含三个元素。
```js
assert.equal(sym([1, 1, 2, 5], [2, 2, 3, 5], [3, 4, 5, 5]).length, 3);
```
`sym([3, 3, 3, 2, 5], [2, 1, 5, 7], [3, 4, 6, 6], [1, 2, 3])`应该返回`[2, 3, 4, 6, 7]` `sym([3, 3, 3, 2, 5], [2, 1, 5, 7], [3, 4, 6, 6], [1, 2, 3])` `[2, 3, 4, 6, 7]`
```js
assert.sameMembers(
sym([3, 3, 3, 2, 5], [2, 1, 5, 7], [3, 4, 6, 6], [1, 2, 3]),
[2, 3, 4, 6, 7]
);
```
`sym([3, 3, 3, 2, 5], [2, 1, 5, 7], [3, 4, 6, 6], [1, 2, 3])`应仅包含五个元素。
```js
assert.equal(
sym([3, 3, 3, 2, 5], [2, 1, 5, 7], [3, 4, 6, 6], [1, 2, 3]).length,
5
);
```
`sym([3, 3, 3, 2, 5], [2, 1, 5, 7], [3, 4, 6, 6], [1, 2, 3], [5, 3, 9, 8], [1])`应该返回`[1, 2, 4, 5, 6, 7, 8, 9]`
```js
assert.sameMembers(
sym(
[3, 3, 3, 2, 5],
[2, 1, 5, 7],
[3, 4, 6, 6],
[1, 2, 3],
[5, 3, 9, 8],
[1]
),
[1, 2, 4, 5, 6, 7, 8, 9]
);
```
`sym([3, 3, 3, 2, 5], [2, 1, 5, 7], [3, 4, 6, 6], [1, 2, 3], [5, 3, 9, 8], [1])`应该只包含八个元素。
```js
assert.equal(
sym([3, 3, 3, 2, 5], [2, 1, 5, 7], [3, 4, 6, 6], [1, 2, 3], [5, 3, 9, 8], [1])
.length,
8
);
```
# --solutions--

View File

@ -1,72 +1,83 @@
---
id: 8d5123c8c441eddfaeb5bdef
title: 实施冒泡排序
challengeType: 1
videoUrl: ''
title: 实施冒泡排序
---
## Description
<section id="description">这是排序算法的几个挑战中的第一个。给定一组未排序的项目,我们希望能够返回已排序的数组。我们将看到几种不同的方法来实现这一点,并学习这些不同方法之间的一些权衡。虽然大多数现代语言都有这样的操作的内置排序方法,但了解一些常见的基本方法并了解如何实现它们仍然很重要。在这里我们将看到冒泡排序。冒泡排序方法从未排序数组的开头开始,并向末端“冒泡”未排序的值,遍历数组直到它完全排序。它通过比较相邻的项目并在它们出现故障时交换它们来完成此操作。该方法继续循环遍历数组,直到没有发生交换,此时数组被排序。该方法需要通过阵列进行多次迭代,并且对于平均和最差情况,具有二次时间复杂度。虽然简单,但在大多数情况下通常都是不切实际的。 <strong>说明:</strong>编写一个函数<code>bubbleSort</code> ,它将整数数组作为输入,并按从最小到最大的排序顺序返回这些整数的数组。 <strong>注意:</strong> <br>我们从幕后调用这个功能;我们使用的测试数组在编辑器中被注释掉了。尝试记录<code>array</code>以查看您的排序算法! </section>
# --description--
## Instructions
<section id="instructions">
</section>
这是排序算法的几个挑战中的第一个。给定一组未排序的项目,我们希望能够返回已排序的数组。我们将看到几种不同的方法来实现这一点,并学习这些不同方法之间的一些权衡。虽然大多数现代语言都有这样的操作的内置排序方法,但了解一些常见的基本方法并了解如何实现它们仍然很重要。在这里我们将看到冒泡排序。冒泡排序方法从未排序数组的开头开始,并向末端“冒泡”未排序的值,遍历数组直到它完全排序。它通过比较相邻的项目并在它们出现故障时交换它们来完成此操作。该方法继续循环遍历数组,直到没有发生交换,此时数组被排序。该方法需要通过阵列进行多次迭代,并且对于平均和最差情况,具有二次时间复杂度。虽然简单,但在大多数情况下通常都是不切实际的。 **说明:** 编写一个函数`bubbleSort` ,它将整数数组作为输入,并按从最小到最大的排序顺序返回这些整数的数组。 **注意:**
我们从幕后调用这个功能;我们使用的测试数组在编辑器中被注释掉了。尝试记录`array`以查看您的排序算法!
## Tests
<section id='tests'>
# --hints--
```yml
tests:
- text: <code>bubbleSort</code>是一个功能。
testString: assert(typeof bubbleSort == 'function');
- text: <code>bubbleSort</code>返回一个已排序的数组(从最小到最大)。
testString: assert(isSorted(bubbleSort([1,4,2,8,345,123,43,32,5643,63,123,43,2,55,1,234,92])));
- text: <code>bubbleSort</code>返回一个除订单外没有变化的数组。
testString: assert.sameMembers(bubbleSort([1,4,2,8,345,123,43,32,5643,63,123,43,2,55,1,234,92]), [1,4,2,8,345,123,43,32,5643,63,123,43,2,55,1,234,92]);
- text: <code>bubbleSort</code>不应使用内置的<code>.sort()</code>方法。
testString: assert.strictEqual(code.search(/\.sort\(/), -1);
```
</section>
## Challenge Seed
<section id='challengeSeed'>
<div id='js-seed'>
`bubbleSort`是一个功能。
```js
function bubbleSort(array) {
// change code below this line
// change code above this line
return array;
}
// test array:
// [1, 4, 2, 8, 345, 123, 43, 32, 5643, 63, 123, 43, 2, 55, 1, 234, 92]
assert(typeof bubbleSort == 'function');
```
</div>
### After Test
<div id='js-teardown'>
`bubbleSort`返回一个已排序的数组(从最小到最大)。
```js
console.info('after the test');
assert(
isSorted(
bubbleSort([
1,
4,
2,
8,
345,
123,
43,
32,
5643,
63,
123,
43,
2,
55,
1,
234,
92
])
)
);
```
</div>
</section>
## Solution
<section id='solution'>
`bubbleSort`返回一个除订单外没有变化的数组。
```js
// solution required
assert.sameMembers(
bubbleSort([
1,
4,
2,
8,
345,
123,
43,
32,
5643,
63,
123,
43,
2,
55,
1,
234,
92
]),
[1, 4, 2, 8, 345, 123, 43, 32, 5643, 63, 123, 43, 2, 55, 1, 234, 92]
);
```
/section>
`bubbleSort`不应使用内置的`.sort()`方法。
```js
assert.strictEqual(code.search(/\.sort\(/), -1);
```
# --solutions--

View File

@ -1,72 +1,83 @@
---
id: 587d8259367417b2b2512c86
title: 实现插入排序
challengeType: 1
videoUrl: ''
title: 实现插入排序
---
## Description
<section id="description">我们将看到的下一个排序方法是插入排序。此方法通过在列表的开头构建排序数组来工作。它以第一个元素开始排序数组。然后它检查下一个元素并将其向后交换到已排序的数组,直到它处于排序位置。它继续遍历列表并将新项目向后交换到已排序的部分,直到它到达结尾。该算法在平均和最差情况下具有二次时间复杂度。 <strong>说明:</strong>编写一个函数<code>insertionSort</code> ,它将一个整数数组作为输入,并按照从最小到最大的排序顺序返回这些整数的数组。 <strong>注意:</strong> <br>我们从幕后调用这个功能;我们使用的测试数组在编辑器中被注释掉了。尝试记录<code>array</code>以查看您的排序算法! </section>
# --description--
## Instructions
<section id="instructions">
</section>
我们将看到的下一个排序方法是插入排序。此方法通过在列表的开头构建排序数组来工作。它以第一个元素开始排序数组。然后它检查下一个元素并将其向后交换到已排序的数组,直到它处于排序位置。它继续遍历列表并将新项目向后交换到已排序的部分,直到它到达结尾。该算法在平均和最差情况下具有二次时间复杂度。 **说明:** 编写一个函数`insertionSort` ,它将一个整数数组作为输入,并按照从最小到最大的排序顺序返回这些整数的数组。 **注意:**
我们从幕后调用这个功能;我们使用的测试数组在编辑器中被注释掉了。尝试记录`array`以查看您的排序算法!
## Tests
<section id='tests'>
# --hints--
```yml
tests:
- text: <code>insertionSort</code>是一个函数。
testString: assert(typeof insertionSort == 'function');
- text: <code>insertionSort</code>返回一个排序数组(从最小到最大)。
testString: assert(isSorted(insertionSort([1,4,2,8,345,123,43,32,5643,63,123,43,2,55,1,234,92])));
- text: <code>insertionSort</code>返回一个除订单外没有变化的数组。
testString: assert.sameMembers(insertionSort([1,4,2,8,345,123,43,32,5643,63,123,43,2,55,1,234,92]), [1,4,2,8,345,123,43,32,5643,63,123,43,2,55,1,234,92]);
- text: <code>insertionSort</code>不应使用内置的<code>.sort()</code>方法。
testString: assert.strictEqual(code.search(/\.sort\(/), -1);
```
</section>
## Challenge Seed
<section id='challengeSeed'>
<div id='js-seed'>
`insertionSort`是一个函数。
```js
function insertionSort(array) {
// change code below this line
// change code above this line
return array;
}
// test array:
// [1, 4, 2, 8, 345, 123, 43, 32, 5643, 63, 123, 43, 2, 55, 1, 234, 92]
assert(typeof insertionSort == 'function');
```
</div>
### After Test
<div id='js-teardown'>
`insertionSort`返回一个排序数组(从最小到最大)。
```js
console.info('after the test');
assert(
isSorted(
insertionSort([
1,
4,
2,
8,
345,
123,
43,
32,
5643,
63,
123,
43,
2,
55,
1,
234,
92
])
)
);
```
</div>
</section>
## Solution
<section id='solution'>
`insertionSort`返回一个除订单外没有变化的数组。
```js
// solution required
assert.sameMembers(
insertionSort([
1,
4,
2,
8,
345,
123,
43,
32,
5643,
63,
123,
43,
2,
55,
1,
234,
92
]),
[1, 4, 2, 8, 345, 123, 43, 32, 5643, 63, 123, 43, 2, 55, 1, 234, 92]
);
```
/section>
`insertionSort`不应使用内置的`.sort()`方法。
```js
assert.strictEqual(code.search(/\.sort\(/), -1);
```
# --solutions--

View File

@ -1,72 +1,83 @@
---
id: 587d825c367417b2b2512c8f
title: 实现合并排序
challengeType: 1
videoUrl: ''
title: 实现合并排序
---
## Description
<section id="description">另一种非常常见的中间排序算法是合并排序。像快速排序一样,合并排序也使用分而治之的递归方法对数组进行排序。它利用了这样一个事实:只要每个数组首先排序,就可以相对容易地对两个数组进行排序。但是我们只从一个数组作为输入开始,那么我们如何从中获得两个排序的数组呢?好吧,我们可以递归地将原始输入分成两部分,直到我们到达具有一个项目的数组的基本情况。单项数组是自然排序的,因此我们可以开始组合。这个组合将展开拆分原始数组的递归调用,最终生成所有元素的最终排序数组。然后,合并排序的步骤是: <strong>1</strong>递归地将输入数组拆分为一半,直到产生仅具有一个元素的子数组。 <strong>2</strong>每个排序的子数组合并在一起以产生最终的排序数组。合并排序是一种有效的排序方法,时间复杂度为<i>Onlogn</i> 。该算法很受欢迎,因为它性能高且易于实现。顺便说一句,这将是我们在此处介绍的最后一种排序算法。但是,稍后在关于树数据结构的部分中,我们将描述堆排序,这是另一种在其实现中需要二进制堆的有效排序方法。 <strong>说明:</strong>编写一个函数<code>mergeSort</code> ,它以整数数组作为输入,并按从最小到最大的排序顺序返回这些整数的数组。实现这一点的一个好方法是编写一个函数,例如<code>merge</code> ,它负责合并两个已排序的数组,另一个函数,例如<code>mergeSort</code> 它负责递归生成单项数组以提供给merge。祝你好运 <strong>注意:</strong> <br>我们从幕后调用这个功能;我们使用的测试数组在编辑器中被注释掉了。尝试记录<code>array</code>以查看您的排序算法! </section>
# --description--
## Instructions
<section id="instructions">
</section>
另一种非常常见的中间排序算法是合并排序。像快速排序一样,合并排序也使用分而治之的递归方法对数组进行排序。它利用了这样一个事实:只要每个数组首先排序,就可以相对容易地对两个数组进行排序。但是我们只从一个数组作为输入开始,那么我们如何从中获得两个排序的数组呢?好吧,我们可以递归地将原始输入分成两部分,直到我们到达具有一个项目的数组的基本情况。单项数组是自然排序的,因此我们可以开始组合。这个组合将展开拆分原始数组的递归调用,最终生成所有元素的最终排序数组。然后,合并排序的步骤是: **1** 递归地将输入数组拆分为一半,直到产生仅具有一个元素的子数组。 **2将** 每个排序的子数组合并在一起以产生最终的排序数组。合并排序是一种有效的排序方法,时间复杂度为*Onlogn* 。该算法很受欢迎,因为它性能高且易于实现。顺便说一句,这将是我们在此处介绍的最后一种排序算法。但是,稍后在关于树数据结构的部分中,我们将描述堆排序,这是另一种在其实现中需要二进制堆的有效排序方法。 **说明:** 编写一个函数`mergeSort` ,它以整数数组作为输入,并按从最小到最大的排序顺序返回这些整数的数组。实现这一点的一个好方法是编写一个函数,例如`merge` ,它负责合并两个已排序的数组,另一个函数,例如`mergeSort` 它负责递归生成单项数组以提供给merge。祝你好运 **注意:**
我们从幕后调用这个功能;我们使用的测试数组在编辑器中被注释掉了。尝试记录`array`以查看您的排序算法!
## Tests
<section id='tests'>
# --hints--
```yml
tests:
- text: <code>mergeSort</code>是一个函数。
testString: assert(typeof mergeSort == 'function');
- text: <code>mergeSort</code>返回一个排序数组(从最小到最大)。
testString: assert(isSorted(mergeSort([1,4,2,8,345,123,43,32,5643,63,123,43,2,55,1,234,92])));
- text: <code>mergeSort</code>返回一个除订单外没有变化的数组。
testString: assert.sameMembers(mergeSort([1,4,2,8,345,123,43,32,5643,63,123,43,2,55,1,234,92]), [1,4,2,8,345,123,43,32,5643,63,123,43,2,55,1,234,92]);
- text: <code>mergeSort</code>不应使用内置的<code>.sort()</code>方法。
testString: assert.strictEqual(code.search(/\.sort\(/), -1);
```
</section>
## Challenge Seed
<section id='challengeSeed'>
<div id='js-seed'>
`mergeSort`是一个函数。
```js
function mergeSort(array) {
// change code below this line
// change code above this line
return array;
}
// test array:
// [1, 4, 2, 8, 345, 123, 43, 32, 5643, 63, 123, 43, 2, 55, 1, 234, 92]
assert(typeof mergeSort == 'function');
```
</div>
### After Test
<div id='js-teardown'>
`mergeSort`返回一个排序数组(从最小到最大)。
```js
console.info('after the test');
assert(
isSorted(
mergeSort([
1,
4,
2,
8,
345,
123,
43,
32,
5643,
63,
123,
43,
2,
55,
1,
234,
92
])
)
);
```
</div>
</section>
## Solution
<section id='solution'>
`mergeSort`返回一个除订单外没有变化的数组。
```js
// solution required
assert.sameMembers(
mergeSort([
1,
4,
2,
8,
345,
123,
43,
32,
5643,
63,
123,
43,
2,
55,
1,
234,
92
]),
[1, 4, 2, 8, 345, 123, 43, 32, 5643, 63, 123, 43, 2, 55, 1, 234, 92]
);
```
/section>
`mergeSort`不应使用内置的`.sort()`方法。
```js
assert.strictEqual(code.search(/\.sort\(/), -1);
```
# --solutions--

View File

@ -1,72 +1,83 @@
---
id: 587d825a367417b2b2512c89
title: 实施快速排序
challengeType: 1
videoUrl: ''
title: 实施快速排序
---
## Description
<section id="description">在这里,我们将继续讨论中间排序算法:快速排序。快速排序是对数组进行排序的一种有效的,递归的分而治之的方法。在此方法中,在原始数组中选择了一个数据透视值。然后将该数组分成两个小于和大于数值的子数组。然后,我们在两个子阵列上结合递归调用快速排序算法的结果。这一直持续到达到空或单项数组的基本情况,我们返回。递归调用的展开将返回已排序的数组。快速排序是一种非常有效的排序方法,平均提供<i>Onlogn</i>性能。它也相对容易实现。这些属性使其成为一种流行且有用的排序方法。 <strong>说明:</strong>编写一个函数<code>quickSort</code> ,它将整数数组作为输入,并按从最小到最大的排序顺序返回这些整数的数组。虽然枢轴值的选择很重要,但任何支点都可以用于我们的目的。为简单起见,可以使用第一个或最后一个元素。 <strong>注意:</strong> <br>我们从幕后调用这个功能;我们使用的测试数组在编辑器中被注释掉了。尝试记录<code>array</code>以查看您的排序算法! </section>
# --description--
## Instructions
<section id="instructions">
</section>
在这里,我们将继续讨论中间排序算法:快速排序。快速排序是对数组进行排序的一种有效的,递归的分而治之的方法。在此方法中,在原始数组中选择了一个数据透视值。然后将该数组分成两个小于和大于数值的子数组。然后,我们在两个子阵列上结合递归调用快速排序算法的结果。这一直持续到达到空或单项数组的基本情况,我们返回。递归调用的展开将返回已排序的数组。快速排序是一种非常有效的排序方法,平均提供*Onlogn*性能。它也相对容易实现。这些属性使其成为一种流行且有用的排序方法。 **说明:** 编写一个函数`quickSort` ,它将整数数组作为输入,并按从最小到最大的排序顺序返回这些整数的数组。虽然枢轴值的选择很重要,但任何支点都可以用于我们的目的。为简单起见,可以使用第一个或最后一个元素。 **注意:**
我们从幕后调用这个功能;我们使用的测试数组在编辑器中被注释掉了。尝试记录`array`以查看您的排序算法!
## Tests
<section id='tests'>
# --hints--
```yml
tests:
- text: <code>quickSort</code>是一个功能。
testString: assert(typeof quickSort == 'function');
- text: <code>quickSort</code>返回一个排序数组(从最小到最大)。
testString: assert(isSorted(quickSort([1,4,2,8,345,123,43,32,5643,63,123,43,2,55,1,234,92])));
- text: <code>quickSort</code>返回一个除订单外没有变化的数组。
testString: assert.sameMembers(quickSort([1,4,2,8,345,123,43,32,5643,63,123,43,2,55,1,234,92]), [1,4,2,8,345,123,43,32,5643,63,123,43,2,55,1,234,92]);
- text: <code>quickSort</code>不应使用内置的<code>.sort()</code>方法。
testString: assert.strictEqual(code.search(/\.sort\(/), -1);
```
</section>
## Challenge Seed
<section id='challengeSeed'>
<div id='js-seed'>
`quickSort`是一个功能。
```js
function quickSort(array) {
// change code below this line
// change code above this line
return array;
}
// test array:
// [1, 4, 2, 8, 345, 123, 43, 32, 5643, 63, 123, 43, 2, 55, 1, 234, 92]
assert(typeof quickSort == 'function');
```
</div>
### After Test
<div id='js-teardown'>
`quickSort`返回一个排序数组(从最小到最大)。
```js
console.info('after the test');
assert(
isSorted(
quickSort([
1,
4,
2,
8,
345,
123,
43,
32,
5643,
63,
123,
43,
2,
55,
1,
234,
92
])
)
);
```
</div>
</section>
## Solution
<section id='solution'>
`quickSort`返回一个除订单外没有变化的数组。
```js
// solution required
assert.sameMembers(
quickSort([
1,
4,
2,
8,
345,
123,
43,
32,
5643,
63,
123,
43,
2,
55,
1,
234,
92
]),
[1, 4, 2, 8, 345, 123, 43, 32, 5643, 63, 123, 43, 2, 55, 1, 234, 92]
);
```
/section>
`quickSort`不应使用内置的`.sort()`方法。
```js
assert.strictEqual(code.search(/\.sort\(/), -1);
```
# --solutions--

View File

@ -1,72 +1,83 @@
---
id: 587d8259367417b2b2512c85
title: 实施选择排序
challengeType: 1
videoUrl: ''
title: 实施选择排序
---
## Description
<section id="description">这里我们将实现选择排序。选择排序的工作原理是选择列表中的最小值并使用列表中的第一个值进行交换。然后它从第二个位置开始,选择剩余列表中的最小值,并将其与第二个元素交换。它继续遍历列表并交换元素,直到它到达列表的末尾。现在列表已排序。在所有情况下,选择排序都具有二次时间复杂度。 <strong>说明</strong> :编写一个函数<code>selectionSort</code> ,它将一个整数数组作为输入,并按照从最小到最大的排序顺序返回这些整数的数组。 <strong>注意:</strong> <br>我们从幕后调用这个功能;我们使用的测试数组在编辑器中被注释掉了。尝试记录<code>array</code>以查看您的排序算法! </section>
# --description--
## Instructions
<section id="instructions">
</section>
这里我们将实现选择排序。选择排序的工作原理是选择列表中的最小值并使用列表中的第一个值进行交换。然后它从第二个位置开始,选择剩余列表中的最小值,并将其与第二个元素交换。它继续遍历列表并交换元素,直到它到达列表的末尾。现在列表已排序。在所有情况下,选择排序都具有二次时间复杂度。 **说明** :编写一个函数`selectionSort` ,它将一个整数数组作为输入,并按照从最小到最大的排序顺序返回这些整数的数组。 **注意:**
我们从幕后调用这个功能;我们使用的测试数组在编辑器中被注释掉了。尝试记录`array`以查看您的排序算法!
## Tests
<section id='tests'>
# --hints--
```yml
tests:
- text: <code>selectionSort</code>是一个函数。
testString: assert(typeof selectionSort == 'function');
- text: <code>selectionSort</code>返回一个排序数组(从最小到最大)。
testString: assert(isSorted(selectionSort([1,4,2,8,345,123,43,32,5643,63,123,43,2,55,1,234,92])));
- text: <code>selectionSort</code>返回一个除订单外没有变化的数组。
testString: assert.sameMembers(selectionSort([1,4,2,8,345,123,43,32,5643,63,123,43,2,55,1,234,92]), [1,4,2,8,345,123,43,32,5643,63,123,43,2,55,1,234,92]);
- text: <code>selectionSort</code>不应使用内置的<code>.sort()</code>方法。
testString: assert.strictEqual(code.search(/\.sort\(/), -1);
```
</section>
## Challenge Seed
<section id='challengeSeed'>
<div id='js-seed'>
`selectionSort`是一个函数。
```js
function selectionSort(array) {
// change code below this line
// change code above this line
return array;
}
// test array:
// [1, 4, 2, 8, 345, 123, 43, 32, 5643, 63, 123, 43, 2, 55, 1, 234, 92]
assert(typeof selectionSort == 'function');
```
</div>
### After Test
<div id='js-teardown'>
`selectionSort`返回一个排序数组(从最小到最大)。
```js
console.info('after the test');
assert(
isSorted(
selectionSort([
1,
4,
2,
8,
345,
123,
43,
32,
5643,
63,
123,
43,
2,
55,
1,
234,
92
])
)
);
```
</div>
</section>
## Solution
<section id='solution'>
`selectionSort`返回一个除订单外没有变化的数组。
```js
// solution required
assert.sameMembers(
selectionSort([
1,
4,
2,
8,
345,
123,
43,
32,
5643,
63,
123,
43,
2,
55,
1,
234,
92
]),
[1, 4, 2, 8, 345, 123, 43, 32, 5643, 63, 123, 43, 2, 55, 1, 234, 92]
);
```
/section>
`selectionSort`不应使用内置的`.sort()`方法。
```js
assert.strictEqual(code.search(/\.sort\(/), -1);
```
# --solutions--

View File

@ -1,80 +1,160 @@
---
id: a56138aff60341a09ed6c480
title: 库存更新
challengeType: 5
videoUrl: ''
title: 库存更新
---
## Description
<section id="description">比较并更新存储在2D阵列中的库存与新交付的第二个2D阵列。更新当前现有库存物料数量<code>arr1</code> )。如果找不到商品,请将新商品和数量添加到库存数组中。返回的库存数组应按项目的字母顺序排列。如果卡住,请记得使用<a href="https://forum.freecodecamp.org/t/how-to-get-help-when-you-are-stuck-coding/19514" target="_blank">Read-Search-Ask</a> 。尝试配对程序。编写自己的代码。 </section>
# --description--
## Instructions
<section id="instructions">
</section>
比较并更新存储在2D阵列中的库存与新交付的第二个2D阵列。更新当前现有库存物料数量`arr1` )。如果找不到商品,请将新商品和数量添加到库存数组中。返回的库存数组应按项目的字母顺序排列。如果卡住,请记得使用[Read-Search-Ask](https://forum.freecodecamp.org/t/how-to-get-help-when-you-are-stuck-coding/19514) 。尝试配对程序。编写自己的代码。
## Tests
<section id='tests'>
# --hints--
```yml
tests:
- text: 函数<code>updateInventory</code>应该返回一个数组。
testString: assert.isArray(updateInventory([[21, "Bowling Ball"], [2, "Dirty Sock"], [1, "Hair Pin"], [5, "Microphone"]], [[2, "Hair Pin"], [3, "Half-Eaten Apple"], [67, "Bowling Ball"], [7, "Toothpaste"]]));
- text: <code>updateInventory([[21, "Bowling Ball"], [2, "Dirty Sock"], [1, "Hair Pin"], [5, "Microphone"]], [[2, "Hair Pin"], [3, "Half-Eaten Apple"], [67, "Bowling Ball"], [7, "Toothpaste"]])</code>应该返回一个长度为6的数组。
testString: assert.equal(updateInventory([[21, "Bowling Ball"], [2, "Dirty Sock"], [1, "Hair Pin"], [5, "Microphone"]], [[2, "Hair Pin"], [3, "Half-Eaten Apple"], [67, "Bowling Ball"], [7, "Toothpaste"]]).length, 6);
- text: <code>updateInventory([[21, "Bowling Ball"], [2, "Dirty Sock"], [1, "Hair Pin"], [5, "Microphone"]], [[2, "Hair Pin"], [3, "Half-Eaten Apple"], [67, "Bowling Ball"], [7, "Toothpaste"]])</code>应返回<code>[[88, "Bowling Ball"], [2, "Dirty Sock"], [3, "Hair Pin"], [3, "Half-Eaten Apple"], [5, "Microphone"], [7, "Toothpaste"]]</code> 。
testString: assert.deepEqual(updateInventory([[21, "Bowling Ball"], [2, "Dirty Sock"], [1, "Hair Pin"], [5, "Microphone"]], [[2, "Hair Pin"], [3, "Half-Eaten Apple"], [67, "Bowling Ball"], [7, "Toothpaste"]]), [[88, "Bowling Ball"], [2, "Dirty Sock"], [3, "Hair Pin"], [3, "Half-Eaten Apple"], [5, "Microphone"], [7, "Toothpaste"]]);
- text: <code>updateInventory([[21, "Bowling Ball"], [2, "Dirty Sock"], [1, "Hair Pin"], [5, "Microphone"]], [])</code>应该返回<code>[[21, "Bowling Ball"], [2, "Dirty Sock"], [1, "Hair Pin"], [5, "Microphone"]]</code> 。
testString: assert.deepEqual(updateInventory([[21, "Bowling Ball"], [2, "Dirty Sock"], [1, "Hair Pin"], [5, "Microphone"]], []), [[21, "Bowling Ball"], [2, "Dirty Sock"], [1, "Hair Pin"], [5, "Microphone"]]);
- text: <code>updateInventory([], [[2, "Hair Pin"], [3, "Half-Eaten Apple"], [67, "Bowling Ball"], [7, "Toothpaste"]])</code>应该返回<code>[[67, "Bowling Ball"], [2, "Hair Pin"], [3, "Half-Eaten Apple"], [7, "Toothpaste"]]</code> 。
testString: assert.deepEqual(updateInventory([], [[2, "Hair Pin"], [3, "Half-Eaten Apple"], [67, "Bowling Ball"], [7, "Toothpaste"]]), [[67, "Bowling Ball"], [2, "Hair Pin"], [3, "Half-Eaten Apple"], [7, "Toothpaste"]]);
- text: <code>updateInventory([[0, "Bowling Ball"], [0, "Dirty Sock"], [0, "Hair Pin"], [0, "Microphone"]], [[1, "Hair Pin"], [1, "Half-Eaten Apple"], [1, "Bowling Ball"], [1, "Toothpaste"]])</code>应返回<code>[[1, "Bowling Ball"], [0, "Dirty Sock"], [1, "Hair Pin"], [1, "Half-Eaten Apple"], [0, "Microphone"], [1, "Toothpaste"]]</code> 。
testString: assert.deepEqual(updateInventory([[0, "Bowling Ball"], [0, "Dirty Sock"], [0, "Hair Pin"], [0, "Microphone"]], [[1, "Hair Pin"], [1, "Half-Eaten Apple"], [1, "Bowling Ball"], [1, "Toothpaste"]]), [[1, "Bowling Ball"], [0, "Dirty Sock"], [1, "Hair Pin"], [1, "Half-Eaten Apple"], [0, "Microphone"], [1, "Toothpaste"]]);
```
</section>
## Challenge Seed
<section id='challengeSeed'>
<div id='js-seed'>
函数`updateInventory`应该返回一个数组。
```js
function updateInventory(arr1, arr2) {
// All inventory must be accounted for or you're fired!
return arr1;
}
// Example inventory lists
var curInv = [
[21, "Bowling Ball"],
[2, "Dirty Sock"],
[1, "Hair Pin"],
[5, "Microphone"]
];
var newInv = [
[2, "Hair Pin"],
[3, "Half-Eaten Apple"],
[67, "Bowling Ball"],
[7, "Toothpaste"]
];
updateInventory(curInv, newInv);
assert.isArray(
updateInventory(
[
[21, 'Bowling Ball'],
[2, 'Dirty Sock'],
[1, 'Hair Pin'],
[5, 'Microphone']
],
[
[2, 'Hair Pin'],
[3, 'Half-Eaten Apple'],
[67, 'Bowling Ball'],
[7, 'Toothpaste']
]
)
);
```
</div>
</section>
## Solution
<section id='solution'>
`updateInventory([[21, "Bowling Ball"], [2, "Dirty Sock"], [1, "Hair Pin"], [5, "Microphone"]], [[2, "Hair Pin"], [3, "Half-Eaten Apple"], [67, "Bowling Ball"], [7, "Toothpaste"]])`应该返回一个长度为6的数组。
```js
// solution required
assert.equal(
updateInventory(
[
[21, 'Bowling Ball'],
[2, 'Dirty Sock'],
[1, 'Hair Pin'],
[5, 'Microphone']
],
[
[2, 'Hair Pin'],
[3, 'Half-Eaten Apple'],
[67, 'Bowling Ball'],
[7, 'Toothpaste']
]
).length,
6
);
```
/section>
`updateInventory([[21, "Bowling Ball"], [2, "Dirty Sock"], [1, "Hair Pin"], [5, "Microphone"]], [[2, "Hair Pin"], [3, "Half-Eaten Apple"], [67, "Bowling Ball"], [7, "Toothpaste"]])`应返回`[[88, "Bowling Ball"], [2, "Dirty Sock"], [3, "Hair Pin"], [3, "Half-Eaten Apple"], [5, "Microphone"], [7, "Toothpaste"]]`
```js
assert.deepEqual(
updateInventory(
[
[21, 'Bowling Ball'],
[2, 'Dirty Sock'],
[1, 'Hair Pin'],
[5, 'Microphone']
],
[
[2, 'Hair Pin'],
[3, 'Half-Eaten Apple'],
[67, 'Bowling Ball'],
[7, 'Toothpaste']
]
),
[
[88, 'Bowling Ball'],
[2, 'Dirty Sock'],
[3, 'Hair Pin'],
[3, 'Half-Eaten Apple'],
[5, 'Microphone'],
[7, 'Toothpaste']
]
);
```
`updateInventory([[21, "Bowling Ball"], [2, "Dirty Sock"], [1, "Hair Pin"], [5, "Microphone"]], [])`应该返回`[[21, "Bowling Ball"], [2, "Dirty Sock"], [1, "Hair Pin"], [5, "Microphone"]]`
```js
assert.deepEqual(
updateInventory(
[
[21, 'Bowling Ball'],
[2, 'Dirty Sock'],
[1, 'Hair Pin'],
[5, 'Microphone']
],
[]
),
[
[21, 'Bowling Ball'],
[2, 'Dirty Sock'],
[1, 'Hair Pin'],
[5, 'Microphone']
]
);
```
`updateInventory([], [[2, "Hair Pin"], [3, "Half-Eaten Apple"], [67, "Bowling Ball"], [7, "Toothpaste"]])`应该返回`[[67, "Bowling Ball"], [2, "Hair Pin"], [3, "Half-Eaten Apple"], [7, "Toothpaste"]]`
```js
assert.deepEqual(
updateInventory(
[],
[
[2, 'Hair Pin'],
[3, 'Half-Eaten Apple'],
[67, 'Bowling Ball'],
[7, 'Toothpaste']
]
),
[
[67, 'Bowling Ball'],
[2, 'Hair Pin'],
[3, 'Half-Eaten Apple'],
[7, 'Toothpaste']
]
);
```
`updateInventory([[0, "Bowling Ball"], [0, "Dirty Sock"], [0, "Hair Pin"], [0, "Microphone"]], [[1, "Hair Pin"], [1, "Half-Eaten Apple"], [1, "Bowling Ball"], [1, "Toothpaste"]])`应返回`[[1, "Bowling Ball"], [0, "Dirty Sock"], [1, "Hair Pin"], [1, "Half-Eaten Apple"], [0, "Microphone"], [1, "Toothpaste"]]`
```js
assert.deepEqual(
updateInventory(
[
[0, 'Bowling Ball'],
[0, 'Dirty Sock'],
[0, 'Hair Pin'],
[0, 'Microphone']
],
[
[1, 'Hair Pin'],
[1, 'Half-Eaten Apple'],
[1, 'Bowling Ball'],
[1, 'Toothpaste']
]
),
[
[1, 'Bowling Ball'],
[0, 'Dirty Sock'],
[1, 'Hair Pin'],
[1, 'Half-Eaten Apple'],
[0, 'Microphone'],
[1, 'Toothpaste']
]
);
```
# --solutions--

View File

@ -1,72 +1,75 @@
---
id: a7bf700cd123b9a54eef01d5
title: 请不要重复
challengeType: 5
videoUrl: ''
title: 请不要重复
---
## Description
<section id="description">返回没有重复连续字母的提供字符串的总排列数。假设提供的字符串中的所有字符都是唯一的。例如, <code>aab</code>应该返回2因为它总共有6个排列 <code>aab</code> <code>aab</code> <code>aba</code> <code>aba</code> <code>baa</code> <code>baa</code> 但只有2个 <code>aba</code><code>aba</code> )没有相同的字母(在这种情况下为<code>a</code> )重复。如果卡住,请记得使用<a href="https://forum.freecodecamp.org/t/how-to-get-help-when-you-are-stuck-coding/19514" target="_blank">Read-Search-Ask</a> 。尝试配对程序。编写自己的代码。 </section>
# --description--
## Instructions
<section id="instructions">
</section>
返回没有重复连续字母的提供字符串的总排列数。假设提供的字符串中的所有字符都是唯一的。例如, `aab`应该返回2因为它总共有6个排列 `aab` `aab` `aba` `aba` `baa` `baa` 但只有2个 `aba``aba` )没有相同的字母(在这种情况下为`a` )重复。如果卡住,请记得使用[Read-Search-Ask](https://forum.freecodecamp.org/t/how-to-get-help-when-you-are-stuck-coding/19514) 。尝试配对程序。编写自己的代码。
## Tests
<section id='tests'>
# --hints--
```yml
tests:
- text: <code>permAlone(&quot;aab&quot;)</code>应返回一个数字。
testString: assert.isNumber(permAlone('aab'));
- text: <code>permAlone(&quot;aab&quot;)</code>应返回2。
testString: assert.strictEqual(permAlone('aab'), 2);
- text: <code>permAlone(&quot;aaa&quot;)</code>应该返回0。
testString: assert.strictEqual(permAlone('aaa'), 0);
- text: <code>permAlone(&quot;aabb&quot;)</code>应该返回8。
testString: assert.strictEqual(permAlone('aabb'), 8);
- text: <code>permAlone(&quot;abcdefa&quot;)</code>应返回3600。
testString: assert.strictEqual(permAlone('abcdefa'), 3600);
- text: <code>permAlone(&quot;abfdefa&quot;)</code>应返回2640。
testString: assert.strictEqual(permAlone('abfdefa'), 2640);
- text: <code>permAlone(&quot;zzzzzzzz&quot;)</code>应该返回0。
testString: assert.strictEqual(permAlone('zzzzzzzz'), 0);
- text: <code>permAlone(&quot;a&quot;)</code>应返回1。
testString: assert.strictEqual(permAlone('a'), 1);
- text: <code>permAlone(&quot;aaab&quot;)</code>应该返回0。
testString: assert.strictEqual(permAlone('aaab'), 0);
- text: <code>permAlone(&quot;aaabb&quot;)</code>应该返回12。
testString: assert.strictEqual(permAlone('aaabb'), 12);
```
</section>
## Challenge Seed
<section id='challengeSeed'>
<div id='js-seed'>
`permAlone("aab")`应返回一个数字。
```js
function permAlone(str) {
return str;
}
permAlone('aab');
assert.isNumber(permAlone('aab'));
```
</div>
</section>
## Solution
<section id='solution'>
`permAlone("aab")`应返回2。
```js
// solution required
assert.strictEqual(permAlone('aab'), 2);
```
/section>
`permAlone("aaa")`应该返回0。
```js
assert.strictEqual(permAlone('aaa'), 0);
```
`permAlone("aabb")`应该返回8。
```js
assert.strictEqual(permAlone('aabb'), 8);
```
`permAlone("abcdefa")`应返回3600。
```js
assert.strictEqual(permAlone('abcdefa'), 3600);
```
`permAlone("abfdefa")`应返回2640。
```js
assert.strictEqual(permAlone('abfdefa'), 2640);
```
`permAlone("zzzzzzzz")`应该返回0。
```js
assert.strictEqual(permAlone('zzzzzzzz'), 0);
```
`permAlone("a")`应返回1。
```js
assert.strictEqual(permAlone('a'), 1);
```
`permAlone("aaab")`应该返回0。
```js
assert.strictEqual(permAlone('aaab'), 0);
```
`permAlone("aaabb")`应该返回12。
```js
assert.strictEqual(permAlone('aaabb'), 12);
```
# --solutions--

View File

@ -1,62 +1,53 @@
---
id: a3f503de51cfab748ff001aa
title: 成对
challengeType: 5
videoUrl: ''
title: 成对
---
## Description
<section id="description">给定一个数组<code>arr</code> ,找到其总和等于第二个参数<code>arg</code>元素对,并返回它们的索引之和。您可以使用具有相同数字元素但索引不同的多个对。每对应使用尽可能低的可用指数。一旦元素被使用,它就不能被重用来与另一个元素配对。例如, <code>pairwise([1, 1, 2], 3)</code>使用indice 0处的1而不是indice 1处的1创建一对<code>[2, 1]</code> 因为0 + 2 &lt;1 + 2。例如 <code>pairwise([7, 9, 11, 13, 15], 20)</code>返回<code>6</code> 。总和为20的对是<code>[7, 13]</code><code>[9, 11]</code> 。然后我们可以用它们的索引和值写出数组。 <table class="table"><tbody><tr><th> <strong>指数</strong> </th><th> 0 </th><th> 1 </th><th> 2 </th><th> 3 </th><th> 4 </th></tr><tr><td></td><td> 7 </td><td> 9 </td><td> 11 </td><td> 13 </td><td> 15 </td></tr></tbody></table>下面我们将采用相应的索引并添加它们。 7 + 13 = 20→指数0 + 3 = 3 <br> 9 + 11 = 20→指数1 + 2 = 3 <br> 3 + 3 = 6→返回<code>6</code>如果卡住,请记住使用<a href="https://forum.freecodecamp.org/t/how-to-get-help-when-you-are-stuck-coding/19514" target="_blank">Read-Search-Ask</a> 。尝试配对程序。编写自己的代码。 </section>
# --description--
## Instructions
<section id="instructions">
</section>
给定一个数组`arr` ,找到其总和等于第二个参数`arg`元素对,并返回它们的索引之和。您可以使用具有相同数字元素但索引不同的多个对。每对应使用尽可能低的可用指数。一旦元素被使用,它就不能被重用来与另一个元素配对。例如, `pairwise([1, 1, 2], 3)`使用indice 0处的1而不是indice 1处的1创建一对`[2, 1]` 因为0 + 2 &lt;1 + 2。例如 `pairwise([7, 9, 11, 13, 15], 20)`返回`6` 。总和为20的对是`[7, 13]``[9, 11]` 。然后我们可以用它们的索引和值写出数组。
## Tests
<section id='tests'>
| **指数** | 0 | 1 | 2 | 3 | 4 |
| ------ | - | - | -- | -- | -- |
| 值 | 7 | 9 | 11 | 13 | 15 |
```yml
tests:
- text: '<code>pairwise([1, 4, 2, 3, 0, 5], 7)</code>应该返回11。'
testString: assert.deepEqual(pairwise([1, 4, 2, 3, 0, 5], 7), 11);
- text: '<code>pairwise([1, 3, 2, 4], 4)</code>应该返回1。'
testString: assert.deepEqual(pairwise([1, 3, 2, 4], 4), 1);
- text: '<code>pairwise([1, 1, 1], 2)</code>应该返回1。'
testString: assert.deepEqual(pairwise([1, 1, 1], 2), 1);
- text: '<code>pairwise([0, 0, 0, 0, 1, 1], 1)</code>应返回10。'
testString: assert.deepEqual(pairwise([0, 0, 0, 0, 1, 1], 1), 10);
- text: '<code>pairwise([], 100)</code>应该返回0。'
testString: assert.deepEqual(pairwise([], 100), 0);
下面我们将采用相应的索引并添加它们。 7 + 13 = 20→指数0 + 3 = 3
9 + 11 = 20→指数1 + 2 = 3
3 + 3 = 6→返回`6`如果卡住,请记住使用[Read-Search-Ask](https://forum.freecodecamp.org/t/how-to-get-help-when-you-are-stuck-coding/19514) 。尝试配对程序。编写自己的代码。
```
# --hints--
</section>
## Challenge Seed
<section id='challengeSeed'>
<div id='js-seed'>
`pairwise([1, 4, 2, 3, 0, 5], 7)`应该返回11。
```js
function pairwise(arr, arg) {
return arg;
}
pairwise([1,4,2,3,0,5], 7);
assert.deepEqual(pairwise([1, 4, 2, 3, 0, 5], 7), 11);
```
</div>
</section>
## Solution
<section id='solution'>
`pairwise([1, 3, 2, 4], 4)`应该返回1。
```js
// solution required
assert.deepEqual(pairwise([1, 3, 2, 4], 4), 1);
```
/section>
`pairwise([1, 1, 1], 2)`应该返回1。
```js
assert.deepEqual(pairwise([1, 1, 1], 2), 1);
```
`pairwise([0, 0, 0, 0, 1, 1], 1)`应返回10。
```js
assert.deepEqual(pairwise([0, 0, 0, 0, 1, 1], 1), 10);
```
`pairwise([], 100)`应该返回0。
```js
assert.deepEqual(pairwise([], 100), 0);
```
# --solutions--