Array.find II - object
Easy
1
33.3% Acceptance
In this coding challenge, you will be implementing a function called getAnAdult(users)
that takes an array of objects users
. This function should return the first object which property age
corresponds to a value greater or equal to 18.
Instructions
- If no such element is found, return
null
. - You can assume that all objects in
users
have the propertyage
.
Example test cases
1const users = [ 2 { name: 'John', age: 15 }, 3 { name: 'Jane', age: 20 }, 4 { name: 'Bob', age: 25 }, 5]; 6getAnAdult(users); // Output: { name: 'Jane', age: 20 } 7 8const users = [ 9 { name: 'John', age: 17 }, 10 { name: 'Jane', age: 17 }, 11 { name: 'Bob', age: 18 }, 12]; 13getAnAdult(users); // Output: { name: 'Bob', age: 18 } 14 15const users = [ 16 { name: 'John', age: 17 }, 17 { name: 'Jane', age: 17 }, 18 { name: 'Bob', age: 17 }, 19]; 20getAnAdult(users); // Output: null
Hints
- You can use the built-in method
.find()
directly on the input array. - Alternatively, you can write a for loop that iterates over every element of the
array
and checks each object'sage
property is greater or equal to 18.