fork download
  1. class Kamus {
  2. constructor() {
  3. this.kamus = {};
  4. }
  5.  
  6. tambah(kata, sinonim) {
  7. if (!this.kamus[kata]) {
  8. this.kamus[kata] = [];
  9. }
  10. this.kamus[kata] = this.kamus[kata].concat(sinonim);
  11. sinonim.forEach((s) => {
  12. if (!this.kamus[s]) {
  13. this.kamus[s] = [];
  14. }
  15. this.kamus[s].push(kata);
  16. });
  17. }
  18.  
  19. ambilSinonim(kata) {
  20. return this.kamus[kata] || null;
  21. }
  22. }
  23.  
  24. const kamus = new Kamus();
  25. kamus.tambah("big", ["large", "great"]);
  26. kamus.tambah("big", ["huge", "fat"]);
  27. kamus.tambah("huge", ["enormous", "gigantic"]);
  28.  
  29. console.log(kamus.ambilSinonim("big"));
  30. console.log(kamus.ambilSinonim("huge"));
  31. console.log(kamus.ambilSinonim("gigantic"));
  32. console.log(kamus.ambilSinonim("colossal"));
Success #stdin #stdout 0.04s 18900KB
stdin
Standard input is empty
stdout
large,great,huge,fat
big,enormous,gigantic
huge
null