fork download
  1. importPackage(java.io);
  2. importPackage(java.lang);
  3.  
  4. // your code goes here
  5.  
  6. var arr = [{name:"adam",age:"21"},
  7. {name:"freddie",age:"35",children:[{name:"mercury",age:"25"}]},
  8. {name:"jim",age:"35",children:[{name:"morrison",age:"25"}]}
  9. ];
  10. var res = new Array([]);
  11. var cp = [];
  12. var deepCopy = function (tree) {
  13. var copied = {};
  14. var res = [];
  15. for (var i = 0; i < tree.length; i++) {
  16. System.out.println(tree[i].name);
  17. if (tree[i].children) {
  18. System.out.println("hello");
  19. var temps = new Object({});
  20. var temp1 = new Array([]);
  21. temps.name =tree[i].name;
  22. temps.age = tree[i].age;
  23. temp1 = deepCopy(tree[i].children);
  24. temps.children = temp1;
  25. res.push(temps);
  26. temps = new Object({});
  27. // System.out.println("child" + temp1.name);
  28. // temp1 = new Array([]);
  29. }
  30. else {
  31.  
  32. var temp = new Object({});
  33. temp.name = tree[i].name;
  34. temp.age = tree[i].age;
  35. // System.out.println("ello");
  36. res.push(temp);
  37. System.out.println("lo");
  38. }
  39. //System.out.println("llo");
  40. }
  41. return res;
  42. }
  43. res = deepCopy(arr);
  44. for (var i = 0; i < arr.length; i++) {
  45. System.out.print(arr[i].name + " ");
  46. }
  47. System.out.println();
  48. //arr[0].name = "sahu";
  49. //arr[1].children.name = "ss";
  50. for (var i = 0; i < arr.length; i++) {
  51. System.out.print(arr[i].name + " ");
  52. }
  53. //System.out.println(arr[1].children.name);
  54. for (var i = 0; i < res.length; i++) {
  55. System.out.print(res[i].name + " ");
  56. }
  57. System.out.println(res[1].children.name);
Runtime error #stdin #stdout #stderr 0.52s 322432KB
stdin
Standard input is empty
stdout
adam
lo
freddie
hello
mercury
lo
jim
hello
morrison
lo
adam freddie jim 
adam freddie jim adam freddie jim 
stderr
js: The choice of Java method java.io.PrintStream.println matching JavaScript argument types (org.mozilla.javascript.Undefined) is ambiguous; candidate methods are: 
    void println(java.lang.Object)
    void println(java.lang.String)