fork download
  1. class Klasemen {
  2. constructor(clubs) {
  3. this.points = {};
  4. for (let club of clubs) {
  5. this.points[club] = 0;
  6. }
  7. }
  8.  
  9. catatPermainan(klubKandang, klubTandang, skor) {
  10. let [goalsHome, goalsAway] = skor.split(':').map(Number);
  11. if (goalsHome > goalsAway) {
  12. this.points[klubKandang] += 3;
  13. } else if (goalsHome < goalsAway) {
  14. this.points[klubTandang] += 3;
  15. } else {
  16. this.points[klubKandang] += 1;
  17. this.points[klubTandang] += 1;
  18. }
  19. }
  20.  
  21. cetakKlasemen() {
  22. let standings = Object.entries(this.points);
  23. standings.sort((a, b) => b[1] - a[1]);
  24. return standings;
  25. }
  26.  
  27. ambilPeringkat(nomorPeringkat) {
  28. let standings = this.cetakKlasemen();
  29. return standings[nomorPeringkat - 1][0];
  30. }
  31. }
  32.  
  33. // Contoh penggunaan:
  34. let klasemen = new Klasemen(['Liverpool', 'Chelsea', 'Arsenal']);
  35. klasemen.catatPermainan('Arsenal', 'Liverpool', '2:1');
  36. klasemen.catatPermainan('Arsenal', 'Chelsea', '1:1');
  37. klasemen.catatPermainan('Chelsea', 'Arsenal', '0:3');
  38. klasemen.catatPermainan('Chelsea', 'Liverpool', '3:2');
  39. klasemen.catatPermainan('Liverpool', 'Arsenal', '2:2');
  40. klasemen.catatPermainan('Liverpool', 'Chelsea', '0:0');
  41.  
  42. console.log(klasemen.cetakKlasemen());
  43. console.log(klasemen.ambilPeringkat(2));
Success #stdin #stdout 0.03s 19668KB
stdin
Standard input is empty
stdout
Arsenal,8,Chelsea,5,Liverpool,2
Chelsea