freeCodeCamp/guide/chinese/certifications/javascript-algorithms-and-d.../functional-programming/use-the-every-method-to-che.../index.md

1.0 KiB
Raw Blame History

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

使用every方法检查数组中的每个元素是否符合条件

问题说明:

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

相关链接:

暗示

不要忘记return

function checkPositive(arr) { 
  // Add your code below this line 
 
  return arr.every(val => val > 0); 
  // Add your code above this line 
 } 
 checkPositive([1, 2, 3, -4, 5]); 

替代方案

function checkPositive(arr) { 
  // Add your code below this line 
    return arr.every(function(value) { 
        return value > 0; 
    }); 
  // Add your code above this line 
 } 
 checkPositive([1, 2, 3, -4, 5]);