2018-10-10 18:03:03 -04:00
|
|
|
---
|
|
|
|
id: a5deed1811a43193f9f1c841
|
|
|
|
challengeType: 5
|
2020-09-07 16:10:29 +08:00
|
|
|
forumTopicId: 16010
|
|
|
|
localeTitle: 筛选出数组中满足条件的元素
|
2018-10-10 18:03:03 -04:00
|
|
|
---
|
|
|
|
|
|
|
|
## Description
|
2020-09-07 16:10:29 +08:00
|
|
|
<section id='description'>
|
|
|
|
给定数组<code>arr</code>,从数组的第一个元素开始,用函数<code>func</code>来检查数组的每个元素并删除,直到某个元素传入函数<code>func</code>时返回<code>true</code>。函数最终的返回值也是一个数组,它由原数组中第一个使得<code>func</code>为<code>true</code>的元素及其之后的所有元素组成。
|
|
|
|
如果数组中的所有元素都不能让<code>func</code>为<code>true</code>,则返回空数组<code>[]</code>。
|
|
|
|
</section>
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
|
|
## Instructions
|
2020-09-07 16:10:29 +08:00
|
|
|
<section id='instructions'>
|
|
|
|
|
2018-10-10 18:03:03 -04:00
|
|
|
</section>
|
|
|
|
|
|
|
|
## Tests
|
|
|
|
<section id='tests'>
|
|
|
|
|
|
|
|
```yml
|
|
|
|
tests:
|
2020-09-07 16:10:29 +08:00
|
|
|
- text: <code>dropElements([1, 2, 3, 4], function(n) {return n >= 3;})</code>应该返回<code>[3, 4]</code>。
|
2020-02-18 01:40:55 +09:00
|
|
|
testString: assert.deepEqual(dropElements([1, 2, 3, 4], function(n) {return n >= 3;}), [3, 4]);
|
2020-09-07 16:10:29 +08:00
|
|
|
- text: <code>dropElements([0, 1, 0, 1], function(n) {return n === 1;})</code>应该返回<code>[1, 0, 1]</code>。
|
2020-02-18 01:40:55 +09:00
|
|
|
testString: assert.deepEqual(dropElements([0, 1, 0, 1], function(n) {return n === 1;}), [1, 0, 1]);
|
2020-09-07 16:10:29 +08:00
|
|
|
- text: <code>dropElements([1, 2, 3], function(n) {return n > 0;})</code>应该返回<code>[1, 2, 3]</code>。
|
2020-02-18 01:40:55 +09:00
|
|
|
testString: assert.deepEqual(dropElements([1, 2, 3], function(n) {return n > 0;}), [1, 2, 3]);
|
2020-09-07 16:10:29 +08:00
|
|
|
- text: <code>dropElements([1, 2, 3, 4], function(n) {return n > 5;})</code>应该返回<code>[]</code>。
|
2020-02-18 01:40:55 +09:00
|
|
|
testString: assert.deepEqual(dropElements([1, 2, 3, 4], function(n) {return n > 5;}), []);
|
2020-09-07 16:10:29 +08:00
|
|
|
- text: <code>dropElements([1, 2, 3, 7, 4], function(n) {return n > 3;})</code>应该返回<code>[7, 4]</code>。
|
2020-02-18 01:40:55 +09:00
|
|
|
testString: assert.deepEqual(dropElements([1, 2, 3, 7, 4], function(n) {return n > 3;}), [7, 4]);
|
2020-09-07 16:10:29 +08:00
|
|
|
- text: <code>dropElements([1, 2, 3, 9, 2], function(n) {return n > 2;})</code>应该返回<code>[3, 9, 2]</code>。
|
2020-02-18 01:40:55 +09:00
|
|
|
testString: assert.deepEqual(dropElements([1, 2, 3, 9, 2], function(n) {return n > 2;}), [3, 9, 2]);
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
</section>
|
|
|
|
|
|
|
|
## Challenge Seed
|
|
|
|
<section id='challengeSeed'>
|
|
|
|
|
|
|
|
<div id='js-seed'>
|
|
|
|
|
|
|
|
```js
|
|
|
|
function dropElements(arr, func) {
|
|
|
|
// Drop them elements.
|
|
|
|
return arr;
|
|
|
|
}
|
|
|
|
|
|
|
|
dropElements([1, 2, 3], function(n) {return n < 3; });
|
|
|
|
```
|
|
|
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
</section>
|
|
|
|
|
|
|
|
## Solution
|
|
|
|
<section id='solution'>
|
|
|
|
|
2020-09-07 16:10:29 +08:00
|
|
|
|
2018-10-10 18:03:03 -04:00
|
|
|
```js
|
2020-09-07 16:10:29 +08:00
|
|
|
function dropElements(arr, func) {
|
|
|
|
// Drop them elements.
|
|
|
|
while (arr.length && !func(arr[0])) {
|
|
|
|
arr.shift();
|
|
|
|
}
|
|
|
|
return arr;
|
|
|
|
}
|
2018-10-10 18:03:03 -04:00
|
|
|
```
|
2020-08-13 17:24:35 +02:00
|
|
|
|
2020-09-07 16:10:29 +08:00
|
|
|
</section>
|