fork download
  1. class Kamus {
  2. constructor() {
  3. this.data = new Map();
  4. }
  5.  
  6. tambah(kata, sinonim) {
  7. // Jika kata belum ada, buat array kosong
  8. if (!this.data.has(kata)) {
  9. this.data.set(kata, []);
  10. }
  11.  
  12. // Tambahkan sinonim baru ke kata yang ada
  13. const existingSinonim = this.data.get(kata);
  14. for (let sin of sinonim) {
  15. if (!existingSinonim.includes(sin)) {
  16. existingSinonim.push(sin);
  17. }
  18. }
  19.  
  20. // Update sinonim yang sudah ada untuk menambahkan kata ini sebagai sinonim mereka
  21. for (let sin of sinonim) {
  22. if (!this.data.has(sin)) {
  23. this.data.set(sin, []);
  24. }
  25. const sinData = this.data.get(sin);
  26. if (!sinData.includes(kata)) {
  27. sinData.push(kata);
  28. }
  29. }
  30. }
  31.  
  32. ambilSinonim(kata) {
  33. if (!this.data.has(kata)) {
  34. return null;
  35. }
  36. return this.data.get(kata);
  37. }
  38. }
  39.  
  40. // Contoh penggunaan sesuai dengan yang diminta
  41. const kamus = new Kamus();
  42. kamus.tambah('big', ['large', 'great']);
  43. kamus.tambah('big', ['huge', 'fat']);
  44. kamus.tambah('huge', ['enormous', 'gigantic']);
  45.  
  46. console.log(kamus.ambilSinonim('big')); // ['large', 'great', 'huge', 'fat']
  47. console.log(kamus.ambilSinonim('huge')); // ['big', 'enormous', 'gigantic']
  48. console.log(kamus.ambilSinonim('gigantic')); // ['huge']
  49. console.log(kamus.ambilSinonim('colossal')); // null
Success #stdin #stdout 0.03s 16748KB
stdin
Standard input is empty
stdout
large,great,huge,fat
big,enormous,gigantic
huge
null