freeCodeCamp/curriculum/challenges/chinese/02-javascript-algorithms-and-data-structures/functional-programming/use-the-every-method-to-check-that-every-element-in-an-array-meets-a-criteria.chinese.md

2.2 KiB
Raw Blame History

id, title, challengeType, videoUrl, localeTitle
id title challengeType videoUrl localeTitle
587d7dab367417b2b2512b6e Use the every Method to Check that Every Element in an Array Meets a Criteria 1 使用every方法检查数组中的每个元素是否符合条件

Description

every方法都使用数组来检查每个元素是否通过了特定的测试。它返回一个布尔值 - 如果所有值都满足条件,则返回true否则返回false 。例如,以下代码将检查numbers数组中的每个元素是否小于10
var numbers = [1,5,8,0,10,11];
numbers.everyfunctioncurrentValue{
return currentValue <10;
};
//返回false

Instructions

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

Tests

tests:
  - text: 您的代码应该使用<code>every</code>方法。
    testString: 'assert(code.match(/\.every/g), "Your code should use the <code>every</code> method.");'
  - 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>.");'
  - 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