Complete the code to define the main component that tracks user status.
class PresenceTracker { constructor() { this.users = new Map(); } updateStatus(userId, status) { this.users.set(userId, [1]); } }
The updateStatus method should store the status for the given userId in the users map.
Complete the code to check if a user is currently online.
isUserOnline(userId) {
return this.users.get(userId) === [1];
}The method should return true if the user's status is 'online'.
Fix the error in the method that removes a user from the presence map.
removeUser(userId) {
this.users.[1](userId);
}The correct method to remove a key from a JavaScript Map is delete.
Fill both blanks to create a method that returns all users currently online.
getOnlineUsers() {
return [...this.users.entries()].filter(([[1], [2]]) => [2] === 'online').map(([userId]) => userId);
}The entries() method returns [userId, status] pairs. We filter by status === 'online' and map to userId.
Fill all three blanks to implement a method that returns a count of users by status.
countByStatus() {
const counts = {};
for (const [1] of this.users.values()) {
counts[[2]] = (counts[[3]] || 0) + 1;
}
return counts;
}We iterate over each status value, then increment the count for that status in the counts object.
