fork download
  1. process.stdin.resume();
  2. process.stdin.setEncoding('utf8');
  3.  
  4. class Klasemen {
  5. constructor(daftarKlub = []) {
  6. this.poin = new Map();
  7. daftarKlub.forEach((k) => this.poin.set(k, 0));
  8. }
  9.  
  10. catatPermainan(klubKandang, klubTandang, skor) {
  11. [klubKandang, klubTandang].forEach((k) => {
  12. if (!this.poin.has(k)) {
  13. throw new Error(`Klub ${k} tidak terdaftar`);
  14. }
  15. });
  16. const [golKandang, golTandang] = skor.split(":").map(Number);
  17.  
  18. // Tambahkan poin
  19. if (golKandang > golTandang) {
  20. this.poin.set(klubKandang, this.poin.get(klubKandang) + 3);
  21. } else if (golKandang < golTandang) {
  22. this.poin.set(klubTandang, this.poin.get(klubTandang) + 3);
  23. } else {
  24. this.poin.set(klubKandang, this.poin.get(klubKandang) + 1);
  25. this.poin.set(klubTandang, this.poin.get(klubTandang) + 1);
  26. }
  27. }
  28.  
  29. cetakKlasemen() {
  30. return Object.fromEntries(
  31. [...this.poin.entries()].sort((a, b) => {
  32. const [klubA, poinA] = a;
  33. const [klubB, poinB] = b;
  34.  
  35. if (poinA !== poinB) return poinB - poinA;
  36. return klubA.localeCompare(klubB, "id");
  37. })
  38. );
  39. }
  40.  
  41. ambilPeringkat(nomorPeringkat) {
  42. if (nomorPeringkat < 1) throw new Error("Nomor peringkat harus >= 1");
  43.  
  44. const klasemenTerurut = this.cetakKlasemen();
  45. const klubKeys = Object.keys(klasemenTerurut);
  46.  
  47. return klubKeys[nomorPeringkat - 1] || "";
  48. }
  49. }
  50. const klasemen = new Klasemen(["Liverpool", "Chelsea", "Arsenal"]);
  51.  
  52. klasemen.catatPermainan("Arsenal", "Liverpool", "2:1");
  53. klasemen.catatPermainan("Arsenal", "Chelsea", "1:1");
  54. klasemen.catatPermainan("Chelsea", "Arsenal", "0:3");
  55. klasemen.catatPermainan("Chelsea", "Liverpool", "3:2");
  56. klasemen.catatPermainan("Liverpool", "Arsenal", "2:2");
  57. klasemen.catatPermainan("Liverpool", "Chelsea", "0:0");
  58. // klasemen.cetakKlasemen();
  59. // klasemen.ambilPeringkat(3)
  60. console.log(klasemen.cetakKlasemen());
  61. console.log(klasemen.ambilPeringkat(2));
  62.  
Success #stdin #stdout 0.07s 40928KB
stdin
Standard input is empty
stdout
{ Arsenal: 8, Chelsea: 5, Liverpool: 2 }
Chelsea