fork download
  1. class Kamus {
  2. constructor() {
  3. this.isi = new Map();
  4. }
  5.  
  6. tambah(kata, sinonim) {
  7. if (!this.isi.has(kata)) {
  8. this.isi.set(kata, new Set());
  9. }
  10.  
  11. for (const persinonim of sinonim) {
  12. this.isi.get(kata).add(persinonim);
  13.  
  14. if (!this.isi.has(persinonim)) {
  15. this.isi.set(persinonim, new Set());
  16. }
  17.  
  18. this.isi.get(persinonim).add(kata);
  19. }
  20. }
  21.  
  22. ambilSinonim(kata) {
  23. if (!this.isi.has(kata)) {
  24. return null;
  25. }
  26.  
  27. return Array.from(this.isi.get(kata));
  28. }
  29. }
  30.  
  31. const kamus = new Kamus();
  32. kamus.tambah('big', ['large', 'great']);
  33. kamus.tambah('big', ['huge', 'fat']);
  34. kamus.tambah('huge', ['enormous', 'gigantic']);
  35.  
  36. console.log(kamus.ambilSinonim('big'));
  37.  
  38. console.log(kamus.ambilSinonim('huge'));
  39.  
  40. console.log(kamus.ambilSinonim('gigantic'));
  41.  
  42. console.log(kamus.ambilSinonim('colossal'));
Success #stdin #stdout 0.06s 40172KB
stdin
Standard input is empty
stdout
[ 'large', 'great', 'huge', 'fat' ]
[ 'big', 'enormous', 'gigantic' ]
[ 'huge' ]
null