Gennadysht %!s(int64=2) %!d(string=hai) anos
pai
achega
b90e3c0221

+ 66 - 0
js/15_Recurs/hw_15_01_HTML_Tree.html

@@ -0,0 +1,66 @@
+<header>
+    HTML Tree
+</header>
+
+<body>
+    <script>
+        function walker(parent) {
+            let str = "";
+            let tagName = parent.tagName;
+            if (tagName) {
+                str = `<${tagName}`;
+                let attrs = parent.attrs;
+                if (attrs) {
+                    for (const attrName in attrs) {
+                        str += ` ${attrName}=${attrs[attrName]}`;
+                    }
+                }
+                str += '>';
+                for (const child of parent.children) {
+                    str += walker(child) //вложенный вызов - вложенный уровень вложенности :-D
+                }
+                str += `</${tagName}>`;
+            }
+            else {
+                str = parent;
+            }
+            return str;
+        }
+        const table = {
+            tagName: 'table',
+            attrs: {
+                border: "1",
+            },
+            children: [
+                {
+                    tagName: 'tr',
+                    children: [
+                        {
+                            tagName: "td",
+                            children: ["1x1"],
+                        },
+                        {
+                            tagName: "td",
+                            children: ["1x2"],
+                        },
+                    ]
+                },
+                {
+                    tagName: 'tr',
+                    children: [
+                        {
+                            tagName: "td",
+                            children: ["2x1"],
+                        },
+                        {
+                            tagName: "td",
+                            children: ["2x2"],
+                        },
+                    ]
+                }
+            ]
+        }
+        document.write(walker(table)); //вернет <table border='1' ....
+
+    </script>
+</body>

+ 70 - 0
js/15_Recurs/hw_15_02_DOOM_Tree.html

@@ -0,0 +1,70 @@
+<header>
+    DOM Tree
+</header>
+
+<body>
+    <script>
+        function walker(parent) {
+            let res = undefined;
+            let tagName = parent.tagName;
+            if (tagName) {
+                res = document.createElement(tagName);
+                let attrs = parent.attrs;
+                if (attrs) {
+                    for (const attrName in attrs) {
+                        res[attrName] = attrs[attrName];
+                    }
+                }
+                for (const child of parent.children) {
+                    let childRes = walker(child)
+                    if (typeof childRes === "object") {
+                        res.append(childRes);
+                    }
+                    else {
+                        res.innerText = childRes;
+                    }
+                }
+            }
+            else {
+                res = parent;
+            }
+            return res;
+        }
+        const table = {
+            tagName: 'table',
+            attrs: {
+                border: "1",
+            },
+            children: [
+                {
+                    tagName: 'tr',
+                    children: [
+                        {
+                            tagName: "td",
+                            children: ["1x1"],
+                        },
+                        {
+                            tagName: "td",
+                            children: ["1x2"],
+                        },
+                    ]
+                },
+                {
+                    tagName: 'tr',
+                    children: [
+                        {
+                            tagName: "td",
+                            children: ["2x1"],
+                        },
+                        {
+                            tagName: "td",
+                            children: ["2x2"],
+                        },
+                    ]
+                }
+            ]
+        }
+        document.body.append(walker(table)); //вернет <table border='1' ....
+
+    </script>
+</body>

+ 68 - 0
js/15_Recurs/hw_15_03_DEEP copy.html

@@ -0,0 +1,68 @@
+<header>
+    DOM Tree
+</header>
+
+<body>
+    <script>
+        function walker(parent) {
+            let res = undefined;
+            let isObject = true;
+            if (Array.isArray(parent)) {
+                res = [];
+            }
+            else if (parent != null && typeof parent === "object") {
+                res = {};
+            }
+            else {
+                res = parent;
+                isObject = false;
+            }
+            if (isObject) {
+                for (el in parent) {
+                    res[el] = walker(parent[el]);
+                }
+            }
+            return res;
+        }
+        const table = {
+            tagName: 'table',
+            attrs: {
+                border: "1",
+            },
+            children: [
+                {
+                    tagName: 'tr',
+                    children: [
+                        {
+                            tagName: "td",
+                            children: ["1x1"],
+                        },
+                        {
+                            tagName: "td",
+                            children: ["1x2"],
+                        },
+                    ]
+                },
+                {
+                    tagName: 'tr',
+                    children: [
+                        {
+                            tagName: "td",
+                            children: ["2x1"],
+                        },
+                        {
+                            tagName: "td",
+                            children: ["2x2"],
+                        },
+                    ]
+                }
+            ]
+        }
+        // document.body.append(walker(table)); //вернет <table border='1' ....
+
+        const arr = [1, "string", null, undefined, { a: 15, b: 10, c: [1, 2, 3, 4], d: undefined, e: true }, true, false]
+        const arr2 = walker(arr) //arr2 и все его вложенные массивы и объекты - другие объекты, которые можно менять без риска поменять что-то в arr
+        const table2 = walker(table) //аналогично
+
+    </script>
+</body>