freeCodeCamp/curriculum/challenges/english/02-javascript-algorithms-and-data-structures/functional-programming/use-the-some-method-to-check-that-any-elements-in-an-array-meet-a-criteria.english.md
Randell Dawson 05f73ca409 fix(curriculum): Convert blockquote elements to triple backtick syntax for JavaScript Algorithms and Data Structures (#35992)
* fix: convert js algorithms and data structures

* fix: revert some blocks back to blockquote

* fix: reverted comparison code block to blockquotes

* fix: change js to json

Co-Authored-By: Oliver Eyton-Williams <ojeytonwilliams@gmail.com>

* fix: convert various section to triple backticks

* fix: Make the formatting consistent for comparisons
2019-05-17 08:20:30 -05:00

2.2 KiB

id, title, challengeType
id title challengeType
587d7dab367417b2b2512b6f Use the some Method to Check that Any Elements in an Array Meet a Criteria 1

Description

The some method works with arrays to check if any element passes a particular test. It returns a Boolean value - true if any of the values meet the criteria, false if not. For example, the following code would check if any element in the numbers array is less than 10:
var numbers = [10, 50, 8, 220, 110, 11];
numbers.some(function(currentValue) {
  return currentValue < 10;
});
// Returns true

Instructions

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

Tests

tests:
  - text: Your code should use the <code>some</code> method.
    testString: assert(code.match(/\.some/g), 'Your code should use the <code>some</code> method.');
  - 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>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

function checkPositive(arr) {
  // Add your code below this line
  return arr.some(elem => elem > 0);
  // Add your code above this line
}
checkPositive([1, 2, 3, -4, 5]);