Filter users by division

Filters users based on their ratings within a specified division.

getUsersInDivision

Parameters

  • users (Array): An array of user objects to filter. Each user object must contain a rating property.

  • division (String): The name of the division to filter users for.

Returns

  • (Array): An array of user objects that belong to the specified division based on their rating.

Errors

  • Throws an Error if:

    • users is not an array.

    • division is not a string.

    • The specified division does not exist in divisions.

    • A user object in users does not have a rating property.

Example

const users = [
    { id: 1, rating: 1500 },
    { id: 2, rating: 900 },
    { id: 3 } // No rating
];

const divisions = {
    Gold: { options: { minPoints: 1000, maxPoints: 2000 } },
    Silver: { options: { minPoints: 500, maxPoints: 999 } }
};

try {
    const goldUsers = getUsersInDivision(users, division: 'Gold');
    console.log(goldUsers);
    // Output: [{ id: 1, rating: 1500 }]

    const silverUsers = getUsersInDivision(users, division: 'Silver');
    console.log(silverUsers);
    // Output: [{ id: 2, rating: 900 }]
} catch (error) {
    console.error(error.message);
}

This function is designed to facilitate the retrieval of users who fall within a specified rating range (minPoints to maxPoints) of a given division. It ensures robust error handling, including validation that each user object in users has a rating property, to maintain reliability and consistency in usage.

Last updated