Vitalii Polishchuk 3 rokov pred
rodič
commit
ecdc66ed75

+ 16 - 0
js/08-js-functions-scopes/index.html

@@ -0,0 +1,16 @@
+<!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="js/main.js"></script>
+</body>
+
+</html>

+ 153 - 0
js/08-js-functions-scopes/js/main.js

@@ -0,0 +1,153 @@
+//ES6
+let a = msg => alert(msg);
+
+let cube = number => number * number * number;
+
+let avg2 = (a, b) => (a + b) / 2;
+
+let sum3 = (a = 0, b = 0, c = 0) => a + b + c;
+
+let intRandom = (a = 0, b = 0) => Math.round(Math.random() * (b - a)) + a;
+
+let greetAll = (...arguments) => {
+    for (let i in arguments) {
+        alert(arguments[i])
+    }
+}
+
+let sum = (...arguments) => {
+    let sumAll = 0
+    for (let i in arguments) {
+        sumAll += arguments[i]
+    }
+    return sumAll;
+}
+
+//sort
+var persons = [
+    { name: "Иван", age: 17 },
+    { name: "Мария", age: 35 },
+    { name: "Алексей", age: 73 },
+    { name: "Яков", age: 12 },
+]
+
+function sort(objectName, keyName, boolean = true) {
+    if (boolean) {
+        objectName.sort((a, b) => a[keyName] > b[keyName] ? 1 : -1)
+    } else {
+        objectName.sort((a, b) => a[keyName] - b[keyName])
+    }
+}
+
+sort(persons, "age"); //сортирует по возрасту по возрастанию
+console.log(persons)
+sort(persons, "name", false); //сортирует по имени по убыванию
+console.log(persons)
+
+//array map
+let arrToNumber = ["1", {}, null, undefined, "500", 700].map(elem => +elem ? +elem : elem);
+
+//array reduce
+let arrResult = ["0", 5, 3, "string", null].reduce((result, current) => {
+    if (typeof current === "number" && current !== null) {
+        return result * current;
+    } else {
+        return result;
+    }
+}, 1)
+
+//object filter
+function filter(obj, func) {
+    let newObj = {};
+
+    for (let key in obj) {
+
+        if (func(key, obj[key])) {
+            newObj[key] = obj[key];
+        }
+    }
+
+    return newObj;
+}
+
+var phone = {
+    brand: "meizu",
+    model: "m2",
+    ram: 2,
+    color: "black",
+};
+
+let a = filter(phone, (key, value) => key == "color" || value == 2);
+console.log(a)
+
+//object map
+function map(obj, func) {
+    let newObj = {};
+
+    for (let key in obj) {
+        newObj[(Object.keys(func(key, obj[key])))[0]] = Object.values(func(key, obj[key]))[0];
+    }
+
+    return newObj;
+}
+
+let obj2 = map({ name: "Иван", age: 17 }, function (key, value) {
+    var result = {};
+    result[key + "_"] = value + "$";
+    return result;
+})
+
+console.log(obj2)// возвращает {name_: "Иван$", age_: "17$"}
+
+//Рекурсия Sum
+let sum = (firstNumber, step, n) => {
+    return n == 1 ? firstNumber : firstNumber + sum(firstNumber + step, step, n - 1);    //сумма арифметической прогресии (начиная с firstNumber, шаг step, n элементов)
+}
+
+//Рекурсия HTML Tree
+var someTree = {
+    tagName: "table", //html tag
+    children: [ //вложенные тэги
+        {
+            tagName: "tr",
+            children: [
+                {
+                    tagName: "td",
+                    text: "some text",
+                },
+                {
+                    tagName: "td",
+                    text: "some text 2",
+                }
+            ]
+        }
+    ],
+    attrs:
+    {
+        border: 1,
+    },
+}
+
+function constructorHtml(obj, parent = obj.tagName, childKeyName = "children") {
+    let elem = document.createElement(parent);
+
+    if (obj[childKeyName]) {
+        for (let child of obj[childKeyName]) {
+            elem.append(constructorHtml(child, parent = child.tagName, childKeyName));
+        }
+    }
+
+    if (obj.attrs) {
+        for (let key in obj.attrs) {
+            elem.setAttribute(key, obj.attrs[key]);
+        }
+    }
+
+    if (obj.text) {
+        elem.innerText = obj.text;
+    }
+
+    return elem;
+}
+
+document.body.append(constructorHtml(someTree));