Junior — Senior
Detect and fix bugs in JavaScript functions
livecode
Task condition
In this fragment of JavaScript code, an object-service is implemented that stores a list of users and allows retrieving only those whose status matches the current filter. However, the implementation contains a logical error, causing the filtering to work incorrectly.
Your task is to analyze the code, identify the cause of the incorrect behavior, and suggest a fix while preserving the overall structure of the object.
const userService = {
currentFilter: 'active',
users: [
{ name: 'Alex', status: 'active' },
{ name: 'Nick', status: 'deleted' },
],
getFilteredUsers: function () {
return this.users.filter(function (user) {
return user.status == this.currentFilter;
});
}
};
console.log(userService.getFilteredUsers());