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.md
Oliver Eyton-Williams ee1e8abd87
feat(curriculum): restore seed + solution to Chinese (#40683)
* feat(tools): add seed/solution restore script

* chore(curriculum): remove empty sections' markers

* chore(curriculum): add seed + solution to Chinese

* chore: remove old formatter

* fix: update getChallenges

parse translated challenges separately, without reference to the source

* chore(curriculum): add dashedName to English

* chore(curriculum): add dashedName to Chinese

* refactor: remove unused challenge property 'name'

* fix: relax dashedName requirement

* fix: stray tag

Remove stray `pre` tag from challenge file.

Signed-off-by: nhcarrigan <nhcarrigan@gmail.com>

Co-authored-by: nhcarrigan <nhcarrigan@gmail.com>
2021-01-12 19:31:00 -07:00

1.7 KiB

id, title, challengeType, forumTopicId, dashedName
id title challengeType forumTopicId dashedName
587d7dab367417b2b2512b6e Use the every Method to Check that Every Element in an Array Meets a Criteria 1 301312 use-the-every-method-to-check-that-every-element-in-an-array-meets-a-criteria

--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.

--hints--

Your code should use the every method.

assert(code.match(/\.every/g));

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

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

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

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

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

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

--seed--

--seed-contents--

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


  // Only change code above this line
}
checkPositive([1, 2, 3, -4, 5]);

--solutions--

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