fork download
  1. function chatSystem(){
  2.  
  3. let user = new Map();
  4. let onlineUsers = new Set();
  5.  
  6. function login(id){
  7. if (!onlineUsers.has(id)){
  8. onlineUsers.add(id);
  9. user.set(id,(user.get(id) || 0) + 1 )
  10. }
  11. }
  12. function logout(id){
  13. if (onlineUsers.has(id)){
  14. onlineUsers.delete(id);
  15. }
  16. }
  17. function isOnline(id){
  18. if (onlineUsers.has(id)){
  19. return true
  20. }else{
  21. return false
  22. }
  23. }
  24. function countOnline(){
  25. return onlineUsers.size
  26. }
  27. function countLogins(id){
  28. return (user.get(id) || 0 )
  29. }
  30. return {
  31. login,
  32. logout,
  33. isOnline,
  34. countOnline,
  35. countLogins
  36. }
  37. }
  38.  
  39. const myChat = chatSystem();
  40. myChat.login(1);
  41. myChat.login(3);
  42. myChat.login(2);
  43. myChat.login(4);
  44. console.log(myChat.countOnline());
  45. myChat.logout(1);
  46. console.log(myChat.countOnline());
  47. myChat.login(1);
  48. console.log(myChat.countLogins(1));
  49. console.log(myChat.isOnline(4));
  50. console.log(myChat.isOnline(1));
  51. myChat.logout(2);
  52. console.log(myChat.isOnline(2));
  53.  
Success #stdin #stdout 0.04s 16540KB
stdin
Standard input is empty
stdout
4
3
2
true
true
false