JavaScript: Return true if the provided predicate function returns true for at least one element in a collection, false otherwise
JavaScript fundamental (ES6 Syntax): Exercise-255 with Solution
Predicate True for Any
Write a JavaScript program that returns true if the provided predicate function returns true for at least one element in a collection, false otherwise.
- Use Array.prototype.some() to test if any elements in the collection return true based on fn.
- Omit the second argument, fn, to use Boolean as a default.
Sample Solution:
JavaScript Code:
Output:
true true
Flowchart:

Live Demo:
For more Practice: Solve these Related Problems:
- Write a JavaScript program that returns true if a provided predicate function returns true for at least one element in a collection.
- Write a JavaScript function that iterates over an array and uses Array.some() to test if any element passes a test.
- Write a JavaScript program that checks a list of values with a predicate and stops at the first true result.
- Write a JavaScript function that takes a predicate and a collection and returns a boolean indicating if any element satisfies the condition.
Go to:
PREV : Generate UUID (Node.js).
NEXT : Approximate Equality Check.
Improve this sample solution and post your code through Disqus
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.