0
0
Expressframework~20 mins

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

Choose your learning style9 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.