Applying Data Filtering and Aggregation with a User Management System in JavaScript

Introduction

Welcome to today's lesson on applying data filtering and aggregation in a real-world scenario using a user management system. We'll start by building a foundational structure that can handle basic user operations. Then, we'll expand it by introducing more advanced functionalities that allow filtering and aggregating user data.

Starter Task Methods

In our starter task, we will implement a class that manages basic operations on a collection of user data, specifically handling adding new users, retrieving user profiles, and updating user profiles.

Here are the starter task methods:

  • addUser(userId, age, country, subscribed) - adds a new user with the specified attributes. Returns true if the user was added successfully and false if a user with the same userId already exists.
  • getUser(userId) - returns the user's profile as an object if the user exists; otherwise, returns null.
  • updateUser(userId, age, country, subscribed) - updates the user's profile based on non-null parameters. Returns true if the user exists and was updated, false otherwise.

Solution for the Starter Task

The JavaScript implementation of our starter task is shown below:

class UserManager {
    constructor() {
        this.users = {};  // Initialize an empty object to store user data
    }
    
    // Method to add a new user
    addUser(userId, age, country, subscribed) {
        if (this.users[userId]) {
            return false;  // Return false if userId already exists
        }
        this.users[userId] = { age, country, subscribed };  // Add user to the users object
        return true;  // Return true to indicate successful addition
    }

    // Method to retrieve a user's profile
    getUser(userId) {
        return this.users[userId] || null;  // Return the user profile or null if the user does not exist
    }

    // Method to update a user's profile
    updateUser(userId, age, country, subscribed) {
        if (!this.users[userId]) {
            return false;  // Return false if the user does not exist
        }
        if (age !== null) {
            this.users[userId].age = age;  // Update age if provided
        }
        if (country !== null) {
            this.users[userId].country = country;  // Update country if provided
        }
        if (subscribed !== null) {
            this.users[userId].subscribed = subscribed;  // Update subscription status if provided
        }
        return true;  // Return true to indicate successful update
    }
}

// Example usage
const um = new UserManager();
console.log(um.addUser("u1", 25, "USA", true));  // true
console.log(um.addUser("u2", 30, "Canada", false));  // true
console.log(um.addUser("u1", 22, "Mexico", true));  // false
console.log(um.getUser("u1"));  // { age: 25, country: "USA", subscribed: true }
console.log(um.updateUser("u1", 26, null, null));  // true
console.log(um.updateUser("u3", 19, "UK", false));  // false

This implementation covers all our starter methods. Let's move forward and introduce more complex functionalities.

Sign up

Join the 1M+ learners on CodeSignal

Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal