Browse Source

functions_scopes_part_2

Iryna Bolbat 2 years ago
parent
commit
d387817091
2 changed files with 141 additions and 0 deletions
  1. 12 0
      js_07_functions_scopes_part_2/index.html
  2. 129 0
      js_07_functions_scopes_part_2/main.js

+ 12 - 0
js_07_functions_scopes_part_2/index.html

@@ -0,0 +1,12 @@
+<!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">
+    <title>Document</title>
+</head>
+<body>
+    <script src="main.js"></script>
+</body>
+</html>

+ 129 - 0
js_07_functions_scopes_part_2/main.js

@@ -0,0 +1,129 @@
+//1.sort
+// let persons = [
+//     {name: "Иван", age: 17},
+//     {name: "Мария", age: 35},
+//     {name: "Алексей", age: 73},
+//     {name: "Яков", age: 12},
+// ];
+
+// function sort (arr, key, boolean = true) {
+//     if(boolean) {
+//         arr.sort(((a, b) => a[key] > b[key] ? 1 : -1));
+//     }
+//     else{
+//         arr.sort(((a, b) => a[key] < b[key] ? 1 : -1));
+//     }
+// }
+
+// sort(persons, "age"); 
+// sort(persons, "name", false); 
+// console.log(persons);
+
+//2.array map
+// let arr = ["1", {}, null, undefined, "500", 700];
+// console.log(arr.map(item => isNaN(item) ? item : parseInt(item) || null));
+
+//3.array reduce
+// let arr = ["0", 5, 3, "string", null];
+// let reduce = arr.reduce (function(a, b) {
+//     return((typeof(b) === 'number') ? (a *= b) : a);
+// }, 1)
+// console.log(reduce);
+
+//4.object filter
+// let phone = {
+//     brand: "meizu",
+//     model: "m2",
+//     ram: 2,
+//     color: "black",
+// };
+
+// function filter (obj, f){
+//     for(let key in obj){
+//         if(!f(key, obj[key])){
+//             delete obj[key];
+//         }
+//     }
+//     return obj;
+// }
+
+// filter(phone, (key, value) => key == "color" || value == 2);
+// console.log(phone);
+
+//5.object map
+// function map (obj, f){
+//     let res = {};
+//     for(let key in obj){
+//         res[key] = obj[key];
+//     }
+//     return res;
+// }
+
+// let objMap = map({name: "Иван", age: 17},function(key,value){
+//     let result = {};
+//     result[key+"_"] = value + "$";
+//     return result;
+// }); 
+
+// console.log(objMap);
+
+//6.Рекурсия
+//Sum
+// function sum (num){
+//     if(num === 0){
+//         return 0;
+//     }
+//     else{
+//         return num + sum(num-1);
+//     }
+// }
+
+// console.log(sum(3));
+
+//HTML Tree
+var someTree = {
+    tagName: "table", //html tag
+    subTags: [ //вложенные тэги
+        {
+        tagName: "tr",
+        subTags: [
+            {
+            tagName: "td",
+            text: "some text",
+            },
+            {
+            tagName: "td",
+            text: "some text 2",
+            }
+        ]
+        }
+    ],
+    attrs: 
+    {
+        border: 1,
+    },
+};
+
+function a (obj){
+    let res = '';
+    for(let elem in obj){
+        let el = obj[elem];
+        if('subTags' in el){
+            res += `<${el['tagName']}> ${a(el['subTags'])} </${el['tagName']}>`;
+        }
+        else{
+            res += `<${el['tagName']}> ${el['text']} </${el['tagName']}>`;
+        }
+    }
+    return res;
+}
+
+let str = '';
+if('tagName' in someTree){
+    str += `<${someTree.tagName} border='1'>`;
+}
+str += a(someTree['subTags']);
+str += `</${someTree.tagName}>`;
+
+console.log(str);
+document.write(str);