freeCodeCamp/curriculum/challenges/chinese/02-javascript-algorithms-an.../functional-programming/use-the-some-method-to-chec...

2.2 KiB
Raw Blame History

id title challengeType videoUrl localeTitle
587d7dab367417b2b2512b6f Use the some Method to Check that Any Elements in an Array Meet a Criteria 1 使用某些方法检查阵列中的任何元素是否符合条件

Description

some方法适用于数组,以检查是否有任何元素通过了特定的测试。它返回一个布尔值 - 如果任何值满足条件,则返回true否则返回false 。例如,以下代码将检查numbers数组中的任何元素是否小于10
var number = [10,50,8,220,110,11];
numbers.somefunctioncurrentValue{
return currentValue <10;
};
//返回true

Instructions

使用checkPositive函数中的some方法检查arr任何元素是否为正数。该函数应返回一个布尔值。

Tests

tests:
  - text: 您的代码应该使用<code>some</code>方法。
    testString: 'assert(code.match(/\.some/g), "Your code should use the <code>some</code> method.");'
  - text: '<code>checkPositive([1, 2, 3, -4, 5])</code>应该返回<code>true</code> 。'
    testString: 'assert(checkPositive([1, 2, 3, -4, 5]), "<code>checkPositive([1, 2, 3, -4, 5])</code> should return <code>true</code>.");'
  - text: '<code>checkPositive([1, 2, 3, 4, 5])</code>应该返回<code>true</code> 。'
    testString: 'assert(checkPositive([1, 2, 3, 4, 5]), "<code>checkPositive([1, 2, 3, 4, 5])</code> should return <code>true</code>.");'
  - text: '<code>checkPositive([-1, -2, -3, -4, -5])</code>应该返回<code>false</code> 。'
    testString: 'assert(!checkPositive([-1, -2, -3, -4, -5]), "<code>checkPositive([-1, -2, -3, -4, -5])</code> should return <code>false</code>.");'

Challenge Seed

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


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

Solution

// solution required