javascriptintermediate10 minutes
Fix the Bug in User Age Filter Function
The provided JavaScript function is intended to filter an array of user objects to return only those aged 18 or older. However, it contains bugs that prevent it from working correctly. Identify and fix the bugs so that the function behaves as expected.
Challenge prompt
You have a function named filterAdults which takes an array of user objects, each with a name and age property. The function should return a new array containing only users who are 18 years old or older. Currently, the function does not return the correct result. Fix the bugs so the function properly filters the users by age.
Guidance
- • Examine the function's syntax and logic carefully.
- • Check how the filter method is used and what is returned inside its callback.
Hints
- • Ensure that the filter callback returns a boolean value.
- • Check if you are comparing the user's age correctly.
Starter code
function filterAdults(users) {
return users.filter(user => {
if (user.age > 18) {
user
}
});
}Expected output
[{ name: 'Alice', age: 22 }, { name: 'Charlie', age: 19 }]
Core concepts
array filteringcallback functionsreturn statementcomparison operators
Challenge a Friend
Send this duel to someone else and see if they can solve it.