Back to Freecodecamp

Use the every Method to Check that Every Element in an Array Meets a Criteria

curriculum/challenges/english/blocks/functional-programming/587d7dab367417b2b2512b6e.md

latest1.4 KB
Original Source

--description--

The every method works with arrays to check if every element passes a particular test. It returns a Boolean value - true if all values meet the criteria, false if not.

For example, the following code would check if every element in the numbers array is less than 10:

js
const numbers = [1, 5, 8, 0, 10, 11];

numbers.every(function(currentValue) {
  return currentValue < 10;
});

The every method would return false here.

--instructions--

Use the every method inside the checkPositive function to check if every element in arr is positive. The function should return a Boolean value.

--hints--

Your code should use the every method.

js
assert(__helpers.removeJSComments(code).match(/\.every/g));

checkPositive([1, 2, 3, -4, 5]) should return false.

js
assert.isFalse(checkPositive([1, 2, 3, -4, 5]));

checkPositive([1, 2, 3, 4, 5]) should return true.

js
assert.isTrue(checkPositive([1, 2, 3, 4, 5]));

checkPositive([1, -2, 3, -4, 5]) should return false.

js
assert.isFalse(checkPositive([1, -2, 3, -4, 5]));

--seed--

--seed-contents--

js
function checkPositive(arr) {
  // Only change code below this line


  // Only change code above this line
}

checkPositive([1, 2, 3, -4, 5]);

--solutions--

js
function checkPositive(arr) {
  return arr.every(num => num > 0);
}