fork download
  1. class Klasemen {
  2. constructor(daftarKlub) {
  3. this.poinKlub = {};
  4. for (let klub of daftarKlub) {
  5. this.poinKlub[klub] = 0;
  6. }
  7. }
  8.  
  9. catatPermainan(klubKandang, klubTandang, skor) {
  10. const [skorKandang, skorTandang] = skor.split(':').map(Number);
  11. if (skorKandang > skorTandang) {
  12. this.poinKlub[klubKandang] += 3;
  13. } else if (skorKandang < skorTandang) {
  14. this.poinKlub[klubTandang] += 3;
  15. } else {
  16. this.poinKlub[klubKandang] += 1;
  17. this.poinKlub[klubTandang] += 1;
  18. }
  19. }
  20.  
  21. cetakKlasemen() {
  22. const klasemen = Object.entries(this.poinKlub)
  23. .sort((a, b) => {
  24. if (b[1] === a[1]) {
  25. return a[0].localeCompare(b[0]);
  26. }
  27. return b[1] - a[1];
  28. });
  29. return Object.fromEntries(klasemen);
  30. }
  31.  
  32. ambilPeringkat(nomorPeringkat) {
  33. const klasemen = this.cetakKlasemen();
  34. const klubUrut = Object.keys(klasemen);
  35. return klubUrut[nomorPeringkat - 1] || null;
  36. }
  37. }
  38.  
  39. const klasemen = new Klasemen(['Liverpool', 'Chelsea', 'Arsenal']);
  40. klasemen.catatPermainan('Arsenal', 'Liverpool', '2:1');
  41. klasemen.catatPermainan('Arsenal', 'Chelsea', '1:1');
  42. klasemen.catatPermainan('Chelsea', 'Arsenal', '0:3');
  43. klasemen.catatPermainan('Chelsea', 'Liverpool', '3:2');
  44. klasemen.catatPermainan('Liverpool', 'Arsenal', '2:2');
  45. klasemen.catatPermainan('Liverpool', 'Chelsea', '0:0');
  46.  
  47. console.log(klasemen.cetakKlasemen());
  48. console.log(klasemen.ambilPeringkat(2));
  49.  
Success #stdin #stdout 0.04s 40396KB
stdin
Standard input is empty
stdout
{ Arsenal: 8, Chelsea: 5, Liverpool: 2 }
Chelsea