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