2018-10-10 18:03:03 -04:00
|
|
|
|
---
|
|
|
|
|
id: 587d7dab367417b2b2512b6f
|
|
|
|
|
challengeType: 1
|
2020-08-05 16:38:04 +08:00
|
|
|
|
forumTopicId: 301314
|
2020-10-01 17:54:21 +02:00
|
|
|
|
title: 使用 some 方法检查数组中是否有元素是否符合条件
|
2018-10-10 18:03:03 -04:00
|
|
|
|
---
|
|
|
|
|
|
|
|
|
|
## Description
|
2020-08-05 16:38:04 +08:00
|
|
|
|
<section id='description'>
|
|
|
|
|
<code>some</code>方法用于检测数组中<em>任何</em>元素是否满足指定条件。如果有一个元素满足条件,返回布尔值<code>true</code>,反之返回<code>false</code>。
|
|
|
|
|
举个例子,下面的代码检测数组<code>numbers</code>中是否有元素小于10:
|
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
var numbers = [10, 50, 8, 220, 110, 11];
|
|
|
|
|
numbers.some(function(currentValue) {
|
|
|
|
|
return currentValue < 10;
|
|
|
|
|
});
|
|
|
|
|
// Returns true
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
</section>
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
|
|
|
|
## Instructions
|
2020-08-05 16:38:04 +08:00
|
|
|
|
<section id='instructions'>
|
|
|
|
|
在<code>checkPositive</code>函数值中使用<code>some</code>检查<code>arr</code>中是否有元素为正数,函数应返回一个布尔值。
|
|
|
|
|
</section>
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
|
|
|
|
## Tests
|
|
|
|
|
<section id='tests'>
|
|
|
|
|
|
|
|
|
|
```yml
|
|
|
|
|
tests:
|
2020-08-05 16:38:04 +08:00
|
|
|
|
- text: 应该使用<code>some</code>method.
|
2020-02-18 01:40:55 +09:00
|
|
|
|
testString: assert(code.match(/\.some/g));
|
2020-08-05 16:38:04 +08:00
|
|
|
|
- text: <code>checkPositive([1, 2, 3, -4, 5])</code>应返回<code>true</code>。
|
2020-02-18 01:40:55 +09:00
|
|
|
|
testString: assert(checkPositive([1, 2, 3, -4, 5]));
|
2020-08-05 16:38:04 +08:00
|
|
|
|
- text: <code>checkPositive([1, 2, 3, 4, 5])</code>应返回<code>true</code>。
|
2020-02-18 01:40:55 +09:00
|
|
|
|
testString: assert(checkPositive([1, 2, 3, 4, 5]));
|
2020-08-05 16:38:04 +08:00
|
|
|
|
- text: <code>checkPositive([-1, -2, -3, -4, -5])</code>应返回<code>false</code>。
|
2020-02-18 01:40:55 +09:00
|
|
|
|
testString: assert(!checkPositive([-1, -2, -3, -4, -5]));
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
</section>
|
|
|
|
|
|
|
|
|
|
## Challenge Seed
|
|
|
|
|
<section id='challengeSeed'>
|
|
|
|
|
|
|
|
|
|
<div id='js-seed'>
|
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
function checkPositive(arr) {
|
|
|
|
|
// Add your code below this line
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Add your code above this line
|
|
|
|
|
}
|
|
|
|
|
checkPositive([1, 2, 3, -4, 5]);
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
</section>
|
|
|
|
|
|
|
|
|
|
## Solution
|
|
|
|
|
<section id='solution'>
|
|
|
|
|
|
|
|
|
|
```js
|
2020-08-05 16:38:04 +08:00
|
|
|
|
function checkPositive(arr) {
|
|
|
|
|
// Add your code below this line
|
|
|
|
|
return arr.some(elem => elem > 0);
|
|
|
|
|
// Add your code above this line
|
|
|
|
|
}
|
|
|
|
|
checkPositive([1, 2, 3, -4, 5]);
|
2018-10-10 18:03:03 -04:00
|
|
|
|
```
|
2020-08-05 16:38:04 +08:00
|
|
|
|
|
2018-10-10 18:03:03 -04:00
|
|
|
|
</section>
|