freeCodeCamp/curriculum/challenges/english/02-javascript-algorithms-and-data-structures/functional-programming/use-the-every-method-to-check-that-every-element-in-an-array-meets-a-criteria.english.md
Valeriy 79d9012432 fix(curriculum): quotes in tests (#18828)
* fix(curriculum): tests quotes

* fix(curriculum): fill seed-teardown

* fix(curriculum): fix tests and remove unneeded seed-teardown
2018-10-20 23:32:47 +05:30

2.1 KiB

id, title, challengeType
id title challengeType
587d7dab367417b2b2512b6e Use the every Method to Check that Every Element in an Array Meets a Criteria 1

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:
var numbers = [1, 5, 8, 0, 10, 11];
numbers.every(function(currentValue) {
  return currentValue < 10;
});
// Returns false

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.

Tests

tests:
  - text: Your code should use the <code>every</code> method.
    testString: assert(code.match(/\.every/g), 'Your code should use the <code>every</code> method.');
  - text: <code>checkPositive([1, 2, 3, -4, 5])</code> should return <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> should return <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> should return <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