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