Browse Source

HW_15 recursion version_1

Graf15 2 years ago
parent
commit
79000d0f05
3 changed files with 204 additions and 0 deletions
  1. 18 0
      js/js_15_recursion/index.html
  2. 129 0
      js/js_15_recursion/index.js
  3. 57 0
      js/js_15_recursion/style.css

+ 18 - 0
js/js_15_recursion/index.html

@@ -0,0 +1,18 @@
+<!DOCTYPE html>
+<html lang="en">
+<head>
+    <meta charset="UTF-8">
+    <meta http-equiv="X-UA-Compatible" content="IE=edge">
+    <meta name="viewport" content="width=device-width, initial-scale=1.0">
+    <link rel="stylesheet" href="style.css">
+    <title>Document</title>
+    
+</head>
+<body>
+    <script>
+        
+    </script>
+</body>
+
+</body>
+</html>

+ 129 - 0
js/js_15_recursion/index.js

@@ -0,0 +1,129 @@
+/* Рекурсия: HTML tree
+Используя решение этого задания сделайте функцию, которая рекурсивно создает HTML-строку из древовидной структуры данных Javascript любой вложенности. Проверьте результат работы функции выводя HTML-строку используя document.write или же какой-то_элемент.innerHTML*/
+
+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"],
+                },
+            ]
+        }
+    ]
+}
+//htmlTree(table) //вернет <table border='1' ....
+
+let result = ""
+function recursion (obj, str="") {
+    console.log(obj.tagName)
+    result += "<" + obj.tagName
+    if (obj.attrs){
+        for (property in obj.attrs) {
+            result += ' ' + property + '="' + obj.attrs[property] + '"'
+        }
+    }
+    result += ">"
+
+    console.log(obj.children)
+    
+    let children
+    if (obj.children){
+
+        children = obj.children
+        for (const obj of children){
+            console.log(obj)
+        if (typeof obj !== 'object') {
+            result += obj
+        }
+        if(typeof obj === 'object') {
+            recursion (obj, str)
+        }
+        }
+
+    }
+
+    result += "</" + obj.tagName + ">"
+}
+
+recursion(table)
+console.log(result)
+
+result =""
+
+const body = {
+    tagName : 'body',
+    children : [
+        {
+            tagName : 'div',
+            children : [
+                {
+                    tagName : 'span',
+                    children : ['Enter a data please:']
+                },
+                {
+                    tagName : 'br'
+                },
+                {
+                    tagName : 'input',
+                    attrs : {
+                        type : 'text',
+                        id : 'name'
+                    }
+                },
+                {
+                    tagName : 'input',
+                    attrs : {
+                        type : 'text',
+                        id : 'surname',
+                    }
+                }
+            ]
+        },
+        {
+            tagName : 'div',
+            children : [
+                {
+                    tagName : 'button',
+                    attrs : {
+                        id : 'ok'
+                    },
+                    children : ['OK']
+                },
+                {
+                    tagName : 'button',
+                    attrs : {
+                        id : 'cancel'
+                    },
+                    children : 'Cancel'
+                },
+            ]
+        }
+    ]
+}
+
+recursion(body)
+console.log(result)

+ 57 - 0
js/js_15_recursion/style.css

@@ -0,0 +1,57 @@
+*,
+*:before,
+*:after {
+  box-sizing: border-box;
+}
+
+body {
+    margin: 0;
+    height: 100vh;
+    background-color: darkgrey;
+
+}
+
+.productCard{
+  padding: 10px;
+  margin: 20px;
+  width: 200px;
+  background-color: azure;
+  border-radius: 10px;
+}
+
+.productName{
+  text-align: center;
+  background-color: rgb(231, 30, 8);
+  padding: 5px;
+  color: white;
+  border-radius: 5px;
+  margin-top: 0px;
+}
+
+.productPrice{
+  text-align: center;
+  padding: 8px;
+  background-color: green;
+  color: white;
+  border-radius: 5px;
+}
+
+.showcase{
+  display: flex;
+  align-items: center;
+  justify-content: center;
+}
+
+.productQuantity{
+  padding: 10px 0px 50px;
+  text-align: center;
+}
+
+.orderSection{
+  display: flex;
+  justify-content: center;
+}
+
+.orderSection * {
+margin: 10px
+}