javascriptintermediate10 minutes

Build a Function to Filter and Transform User Data

Create a JavaScript function that takes an array of user objects and returns a new array containing only users above a certain age, with their full names combined into a single string.

Challenge prompt

Write a function named filterAndFormatUsers that accepts two parameters: an array of user objects and a minimum age number. Each user object has properties: firstName, lastName, and age. The function should return a new array of strings where each string is the full name of users whose age is strictly greater than the given minimum age. The full name should be formatted as 'FirstName LastName'.

Guidance

  • Filter the users array to include only those with age greater than the minimum age.
  • Map the filtered users to a new array of full name strings.
  • Return the resulting array of full names.

Hints

  • Use the array methods filter and map for concise and readable code.
  • Remember to concatenate firstName and lastName with a space in between.
  • Check for strict greater than when comparing ages.

Starter code

function filterAndFormatUsers(users, minAge) {
  // Your code here
}

Expected output

filterAndFormatUsers([ { firstName: 'Alice', lastName: 'Johnson', age: 25 }, { firstName: 'Bob', lastName: 'Smith', age: 19 }, { firstName: 'Carol', lastName: 'Taylor', age: 30 } ], 20); // Expected output: ['Alice Johnson', 'Carol Taylor']

Core concepts

array filteringarray mappingobjectsfunctions

Challenge a Friend

Send this duel to someone else and see if they can solve it.