JS Built-ins
Algorithmsintermediate

Find by Predicate

πŸ“– Usage Example
const users = [{ name: 'Alice', age: 30 }, { name: 'Bob', age: 25 }];
users.find(u => u.age < 28);
// { name: 'Bob', age: 25 }

Array.find() returns the first element matching a predicate function.

Find by Predicate

This exercise focuses on finding an element that matches a condition (predicate). The Array.prototype.find() method returns the first element that satisfies the condition.

function findEven(arr: number[]): number | undefined {
  // Return the first even number in the array
}

findEven([1, 3, 4, 5]) // 4
findEven([1, 3, 5]) // undefined
πŸ’‘ Hints (4)
  • β€’ Use the `find()` method with a predicate function.
  • β€’ The predicate should return true for elements matching the condition.
  • β€’ For even numbers, check if `n % 2 === 0`.
  • β€’ `find()` returns undefined if no element matches.