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