1.9 KiB
1.9 KiB
id, challengeType, forumTopicId, localeTitle
id | challengeType | forumTopicId | localeTitle |
---|---|---|---|
587d7dab367417b2b2512b6e | 1 | 301312 | 使用 every 方法检查数组中的每个元素是否符合条件 |
Description
every
方法用于检测数组中所有元素是否都符合指定条件。如果所有元素满足条件,返回布尔值true
,反之返回false
。
举个例子,下面的代码检测数组numbers
的所有元素是否都小于 10:
var numbers = [1, 5, 8, 0, 10, 11];
numbers.every(function(currentValue) {
return currentValue < 10;
});
// Returns false
Instructions
checkPositive
函数中使用every
方法检查arr
中是否所有元素都是正数,函数应返回一个布尔值。
Tests
tests:
- text: 应使用<code>every</code>方法。
testString: assert(code.match(/\.every/g));
- text: <code>checkPositive([1, 2, 3, -4, 5])</code>应返回<code>false</code>。
testString: assert.isFalse(checkPositive([1, 2, 3, -4, 5]));
- text: <code>checkPositive([1, 2, 3, 4, 5])</code>应返回<code>true</code>。
testString: assert.isTrue(checkPositive([1, 2, 3, 4, 5]));
- text: <code>checkPositive([1, -2, 3, -4, 5])</code>应返回<code>false</code>。
testString: assert.isFalse(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.every(num => num > 0);
// Add your code above this line
}
checkPositive([1, 2, 3, -4, 5]);