fork download
  1. function Chat() {
  2. const loggingUsers = new Set();
  3. const loginCount = new Map();
  4.  
  5. const login = (id) => {
  6. if (loggingUsers.has(id)) {
  7. console.log(`User ${id} is already logged in.`);
  8. } else {
  9. console.log(`User ${id} has logged in.`);
  10. loggingUsers.add(id);
  11. if (!loginCount.has(id)) loginCount.set(id, 0);
  12. }
  13. };
  14.  
  15. const logout = (id) => {
  16. if (!loggingUsers.has(id)) {
  17. console.log(`User ${id} is not logged in.`);
  18. } else {
  19. console.log(`User ${id} has logged out.`);
  20. loggingUsers.delete(id);
  21. loginCount.set(id, loginCount.get(id) + 1);
  22. }
  23. };
  24.  
  25. const isOnline = (id) => loggingUsers.has(id);
  26. const countOnline = () => loggingUsers.size;
  27. const countLogins = (id) => loginCount.get(id);
  28.  
  29. return {
  30. login,
  31. logout,
  32. isOnline,
  33. countOnline,
  34. countLogins,
  35. };
  36. }
  37.  
  38. const myChat = Chat();
  39. myChat.login(3);
  40. myChat.logout(3);
  41. myChat.login(2);
  42. myChat.login(1);
  43. myChat.login(2); // already logged in
  44. myChat.logout(3); // not logged in
  45. console.log(myChat.countOnline()); // 2
  46. console.log(myChat.countLogins(3)); // 1
  47.  
Success #stdin #stdout 0.09s 31524KB
stdin
Standard input is empty
stdout
User 3 has logged in.
User 3 has logged out.
User 2 has logged in.
User 1 has logged in.
User 2 is already logged in.
User 3 is not logged in.
2
1