freeCodeCamp/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/functional-programming/use-the-some-method-to-check-that-any-elements-in-an-array-meet-a-criteria.chinese.md
ZhichengChen 1046e21a90
fix(i18n): update Chinese translation of functional programming (#38061)
* fix(i18n): update Chinese translation of functional programming

* fix(i18n): update review suggestion

Co-authored-by: Zhicheng Chen <chenzhicheng@dayuwuxian.com>
2020-08-05 14:08:04 +05:30

1.9 KiB
Raw Blame History

id, title, challengeType, forumTopicId, localeTitle
id title challengeType forumTopicId localeTitle
587d7dab367417b2b2512b6f Use the some Method to Check that Any Elements in an Array Meet a Criteria 1 301314 使用 some 方法检查数组中是否有元素是否符合条件

Description

some方法用于检测数组中任何元素是否满足指定条件。如果有一个元素满足条件,返回布尔值true,反之返回false。 举个例子,下面的代码检测数组numbers中是否有元素小于10
var numbers = [10, 50, 8, 220, 110, 11];
numbers.some(function(currentValue) {
  return currentValue < 10;
});
// Returns true

Instructions

checkPositive函数值中使用some检查arr中是否有元素为正数,函数应返回一个布尔值。

Tests

tests:
  - text: 应该使用<code>some</code>method.
    testString: assert(code.match(/\.some/g));
  - text: <code>checkPositive([1, 2, 3, -4, 5])</code>应返回<code>true</code>。
    testString: assert(checkPositive([1, 2, 3, -4, 5]));
  - text: <code>checkPositive([1, 2, 3, 4, 5])</code>应返回<code>true</code>。
    testString: assert(checkPositive([1, 2, 3, 4, 5]));
  - text: <code>checkPositive([-1, -2, -3, -4, -5])</code>应返回<code>false</code>。
    testString: assert(!checkPositive([-1, -2, -3, -4, -5]));

Challenge Seed

function checkPositive(arr) {
  // Add your code below this line


  // Add your code above this line
}
checkPositive([1, 2, 3, -4, 5]);

Solution

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]);