Bird
Raised Fist0
Expressframework~20 mins

Repository pattern for data access in Express - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Repository Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate
2:00remaining
Repository Pattern: Data Fetching Behavior
Given the repository pattern implementation below, what will be the output when calling userRepository.getById(2) if the data source contains users with ids 1 and 2?
Express
class UserRepository {
  constructor() {
    this.users = [
      { id: 1, name: 'Alice' },
      { id: 2, name: 'Bob' }
    ];
  }

  getById(id) {
    return this.users.find(user => user.id === id);
  }
}

const userRepository = new UserRepository();
console.log(userRepository.getById(2));
A{ id: 2, name: 'Bob' }
Bundefined
C{ id: 1, name: 'Alice' }
DThrows TypeError
Attempts:
2 left
💡 Hint
Look at how the getById method searches the array.
📝 Syntax
intermediate
2:00remaining
Correct Repository Method Syntax
Which option correctly defines a repository method addUser that adds a user object to the internal array users?
Express
class UserRepository {
  constructor() {
    this.users = [];
  }

  // Define addUser method here
}
AaddUser(user) => { this.users.push(user); }
BaddUser = (user) => { this.users.push(user); }
CaddUser(user) { this.users.push(user) }
DaddUser(user) { this.users.push(user); }
Attempts:
2 left
💡 Hint
Check the correct method syntax inside a class in JavaScript.
🔧 Debug
advanced
2:00remaining
Identify the Error in Repository Method
What error will occur when running the following repository method removeUser if called with an id that does not exist in the users array?
Express
class UserRepository {
  constructor() {
    this.users = [{ id: 1, name: 'Alice' }];
  }

  removeUser(id) {
    const index = this.users.findIndex(user => user.id === id);
    this.users.splice(index, 1);
  }
}

const repo = new UserRepository();
repo.removeUser(2);
ARemoves user with id 1
BThrows RangeError due to invalid splice index
CThrows TypeError because index is undefined
DRemoves no user, no error thrown
Attempts:
2 left
💡 Hint
What does findIndex return if no match is found?
❓ state_output
advanced
2:00remaining
State of Repository After Multiple Operations
After running the following code, what is the content of repo.users?
Express
class UserRepository {
  constructor() {
    this.users = [];
  }

  addUser(user) {
    this.users.push(user);
  }

  clearUsers() {
    this.users = [];
  }
}

const repo = new UserRepository();
repo.addUser({ id: 1, name: 'Alice' });
repo.addUser({ id: 2, name: 'Bob' });
repo.clearUsers();
repo.addUser({ id: 3, name: 'Charlie' });
A[{ id: 1, name: 'Alice' }, { id: 2, name: 'Bob' }, { id: 3, name: 'Charlie' }]
B[{ id: 3, name: 'Charlie' }]
C[]
D[{ id: 1, name: 'Alice' }, { id: 2, name: 'Bob' }]
Attempts:
2 left
💡 Hint
Look at what clearUsers does to the users array.
🧠 Conceptual
expert
2:00remaining
Purpose of Repository Pattern in Express Apps
Which option best describes the main benefit of using the repository pattern for data access in an Express application?
AIt replaces the need for middleware in Express routes.
BIt automatically caches all database queries to improve performance.
CIt separates data access logic from business logic, making code easier to maintain and test.
DIt enforces a strict schema on all data models without external libraries.
Attempts:
2 left
💡 Hint
Think about how separating concerns helps in coding.

Practice

(1/5)
1. What is the main purpose of using the Repository pattern in an Express app?
easy
A. To manage user authentication
B. To handle HTTP requests and responses
C. To style the frontend components
D. To separate data access logic from business logic

Solution

  1. Step 1: Understand the role of repository pattern

    The repository pattern is designed to isolate data access logic from the rest of the application.
  2. Step 2: Identify the correct purpose in Express context

    In Express apps, it helps keep business logic clean by moving database queries into repositories.
  3. Final Answer:

    To separate data access logic from business logic -> Option D
  4. Quick Check:

    Repository pattern = separate data access [OK]
Hint: Repository pattern means separate data access from app logic [OK]
Common Mistakes:
  • Confusing repository with routing or middleware
  • Thinking it handles UI or styling
  • Mixing it with authentication logic
2. Which of the following is the correct way to define a repository class in Express using ES6 syntax?
easy
A. class UserRepository { getAll() { /* code */ } }
B. var UserRepository = new Object({ getAll: function() {} })
C. const UserRepository = () => { return { getAll() {} } }
D. function UserRepository() { this.getAll = () => {}; }

Solution

  1. Step 1: Identify ES6 class syntax

    ES6 classes use the class keyword with methods inside without the function keyword.
  2. Step 2: Match the correct syntax

    class UserRepository { getAll() { /* code */ } } correctly defines a class with a method getAll() using ES6 syntax.
  3. Final Answer:

    class UserRepository { getAll() { /* code */ } } -> Option A
  4. Quick Check:

    ES6 class syntax = class UserRepository { getAll() { /* code */ } } [OK]
Hint: ES6 classes use 'class' keyword and method shorthand [OK]
Common Mistakes:
  • Using function keyword inside class methods
  • Confusing arrow functions with class methods
  • Using old object syntax instead of class
3. Given this repository method, what will console.log(users) output?
class UserRepository {
  constructor() {
    this.users = [{id:1, name:'Anna'}, {id:2, name:'Bob'}];
  }
  getAll() {
    return this.users;
  }
}
const repo = new UserRepository();
const users = repo.getAll();
console.log(users);
medium
A. [{id:1, name:'Anna'}, {id:2, name:'Bob'}]
B. undefined
C. Error: getAll is not a function
D. []

Solution

  1. Step 1: Understand the constructor initialization

    The constructor sets this.users to an array of two user objects.
  2. Step 2: Check the getAll method return value

    The getAll() method returns this.users, so calling it returns the array.
  3. Final Answer:

    [{id:1, name:'Anna'}, {id:2, name:'Bob'}] -> Option A
  4. Quick Check:

    getAll returns users array [OK]
Hint: getAll returns stored users array directly [OK]
Common Mistakes:
  • Assuming getAll returns undefined or empty
  • Confusing method call with property access
  • Expecting an error due to missing binding
4. Identify the error in this repository code snippet:
class ProductRepository {
  constructor() {
    this.products = [];
  }
  add(product) {
    this.products.push(product);
  }
  getAll() {
    return products;
  }
}
medium
A. Missing return statement in getAll method
B. Using undefined variable 'products' instead of 'this.products'
C. Constructor does not initialize products array
D. add method does not push product correctly

Solution

  1. Step 1: Check variable usage in getAll method

    The method returns products but this variable is not defined in the method scope.
  2. Step 2: Correct usage of instance variable

    It should return this.products to access the class property.
  3. Final Answer:

    Using undefined variable 'products' instead of 'this.products' -> Option B
  4. Quick Check:

    Use this.products to access class property [OK]
Hint: Use 'this.' to access class properties inside methods [OK]
Common Mistakes:
  • Forgetting 'this.' prefix for class properties
  • Assuming variables are global inside class methods
  • Ignoring constructor initialization
5. You want to extend a base repository to add a custom method findByEmail(email) for users. Which approach correctly implements this in Express?
hard
A. const UserRepository = Object.create(BaseRepository); UserRepository.findByEmail = function(email) { return this.data.find(u => u.email === email); };
B. function UserRepository() { this.findByEmail = (email) => this.data.find(u => u.email === email); } UserRepository.prototype = BaseRepository.prototype;
C. class UserRepository extends BaseRepository { findByEmail(email) { return this.data.find(u => u.email === email); } }
D. class UserRepository { findByEmail(email) { return BaseRepository.data.find(u => u.email === email); } }

Solution

  1. Step 1: Understand class inheritance in ES6

    Extending a base class uses extends keyword and super() if constructor is needed.
  2. Step 2: Check method implementation accessing instance data

    class UserRepository extends BaseRepository { findByEmail(email) { return this.data.find(u => u.email === email); } } correctly extends BaseRepository and uses this.data to find user by email.
  3. Final Answer:

    class UserRepository extends BaseRepository { findByEmail(email) { return this.data.find(u => u.email === email); } } -> Option C
  4. Quick Check:

    Extend base class and add method using this.data [OK]
Hint: Use 'extends' to inherit and add custom methods in subclass [OK]
Common Mistakes:
  • Not using 'extends' for inheritance
  • Accessing base class data incorrectly
  • Overwriting prototype without proper inheritance
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard