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