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