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