Gennadysht 2 vuotta sitten
vanhempi
commit
0ed572ec19

js/15_Recurs/hw_15_02_DOOM_Tree.html → js/15_Recurs/hw_15_02_DOM_Tree.html


+ 1 - 1
js/15_Recurs/hw_15_03_DEEP copy.html

@@ -1,5 +1,5 @@
 <header>
-    DOM Tree
+    Deep Copy
 </header>
 
 <body>

+ 76 - 0
js/15_Recurs/hw_15_04_1_string .html

@@ -0,0 +1,76 @@
+<header>
+    HTML Tree
+</header>
+
+<body>
+    <script>
+        function walker(parent) {
+            let str = "";
+            let tagName = parent.tagName;
+            if (tagName) {
+                str = '{';
+                str += `"tagName":"${tagName}",`;
+                let attrs = parent.attrs;
+                if (attrs) {
+                    str += '"attrs":{';
+                    let attrsArr = [];
+                    for (const attrName in attrs) {
+                        attrsArr.push(`"${attrName}":"${attrs[attrName]}"`);
+                    }
+                    str += attrsArr.join(',');
+                    str += '},';
+                }
+                if (parent.children) {
+                    str += '"children":['
+                    let childrenArr = [];
+                    for (const child of parent.children) {
+                        childrenArr.push(walker(child)); //вложенный вызов - вложенный уровень вложенности :-D
+                    }
+                    str += childrenArr.join(',');
+                    str += ']';
+                }
+                str += '}';
+            }
+            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"],
+                        },
+                    ]
+                }
+            ]
+        }
+        var objStr = walker(table);;
+        document.write(walker(objStr));
+    </script>
+</body>

+ 76 - 0
js/15_Recurs/hw_15_04_string.html

@@ -0,0 +1,76 @@
+<header>
+    HTML Tree
+</header>
+
+<body>
+    <script>
+        function walker(parent) {
+            let str = "";
+            let tagName = parent.tagName;
+            if (tagName) {
+                str = '{';
+                str += `"tagName":"${tagName}",`;
+                let attrs = parent.attrs;
+                if (attrs) {
+                    str += '"attrs":{';
+                    let attrsArr = [];
+                    for (const attrName in attrs) {
+                        attrsArr.push(`"${attrName}":"${attrs[attrName]}"`);
+                    }
+                    str += attrsArr.join(',');
+                    str += '},';
+                }
+                if (parent.children) {
+                    str += '"children":['
+                    let childrenArr = [];
+                    for (const child of parent.children) {
+                        childrenArr.push(walker(child)); //вложенный вызов - вложенный уровень вложенности :-D
+                    }
+                    str += childrenArr.join(',');
+                    str += ']';
+                }
+                str += '}';
+            }
+            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"],
+                        },
+                    ]
+                }
+            ]
+        }
+        var objStr = walker(table);;
+        document.write(walker(objStr));
+    </script>
+</body>

+ 46 - 0
js/15_Recurs/hw_15_05_get.html

@@ -0,0 +1,46 @@
+<header>
+    GetElementById Throw
+</header>
+
+<body>
+    <div id="xId">
+        <div id="testId">testDiv</div>
+    </div>
+    <script>
+        const getElementById = (parent, id) => {
+            try {
+                walker(parent, id);
+                return null;
+            }
+            catch (e) {
+                if (e.message == "Element found.")
+                    return e.result;
+                else
+                    throw e;
+            }
+        }
+        function walker(parent, id) {
+            let isObject = true;
+            if (Array.isArray(parent)) {
+            }
+            else if (parent != null && typeof parent === "object") {
+                if (parent.id == id) {
+                    let err = new Error("Element found.");
+                    err.result = parent;
+                    throw err;
+                }
+            }
+            else
+                isObject = false;
+            if (isObject) {
+                for (child of parent.children) {
+                    walker(child, id);
+                }
+            }
+        }
+        var children = xId.children;
+        var val = xId.value;
+        el = getElementById(document.body, "testId");
+        alert(el.innerText);
+    </script>
+</body>