fork download
  1. process.stdin.resume();
  2. process.stdin.setEncoding('utf8');
  3.  
  4. class Klasemen {
  5. constructor(listKlub) {
  6. this.poin = {};
  7. listKlub.forEach(k => this.poin[k] = 0);
  8. }
  9.  
  10. catatPermainan(klubKandang, klubTandang, skor) {
  11. const [golKandang, golTandang] = skor.split(':').map(Number);
  12. if (golKandang > golTandang) {
  13. this.poin[klubKandang] += 3;
  14. } else if (golTandang > golKandang) {
  15. this.poin[klubTandang] += 3;
  16. } else {
  17. this.poin[klubKandang] += 1;
  18. this.poin[klubTandang] += 1;
  19. }
  20. }
  21.  
  22. cetakKlasemen() {
  23. const hasil = Object.entries(this.poin)
  24. .sort((a, b) => b[1] - a[1])
  25. .reduce((acc, [klub, poin]) => {
  26. acc[klub] = poin;
  27. return acc;
  28. }, {});
  29. return hasil;
  30. }
  31.  
  32. ambilPeringkat(nomorPeringkat) {
  33. const sorted = Object.entries(this.poin)
  34. .sort((a, b) => b[1] - a[1]);
  35. if (nomorPeringkat < 1 || nomorPeringkat > sorted.length) return null;
  36. return sorted[nomorPeringkat - 1][0];
  37. }
  38. }
  39.  
  40. const klasemen = new Klasemen(['Liverpool', 'Chelsea', 'Arsenal']);
  41.  
  42. klasemen.catatPermainan('Arsenal', 'Liverpool', '2:1');
  43. klasemen.catatPermainan('Arsenal', 'Chelsea', '1:1');
  44. klasemen.catatPermainan('Chelsea', 'Arsenal', '0:3');
  45. klasemen.catatPermainan('Chelsea', 'Liverpool', '3:2');
  46. klasemen.catatPermainan('Liverpool', 'Arsenal', '2:2');
  47. klasemen.catatPermainan('Liverpool', 'Chelsea', '0:0');
  48.  
  49. console.log(klasemen.cetakKlasemen());
  50. console.log(klasemen.ambilPeringkat(2));
  51.  
Success #stdin #stdout 0.09s 41896KB
stdin
Standard input is empty
stdout
{ Arsenal: 8, Chelsea: 5, Liverpool: 2 }
Chelsea