Helen 3 years ago
parent
commit
f20fdde9bb
2 changed files with 154 additions and 0 deletions
  1. 12 0
      Функции 2/index.html
  2. 142 0
      Функции 2/js/script.js

+ 12 - 0
Функции 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>HW7</title>
+</head>
+<body>
+    <script src="js/script.js"></script>
+</body>
+</html>

+ 142 - 0
Функции 2/js/script.js

@@ -0,0 +1,142 @@
+"use strict";
+
+//TODO: 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;
+// }
+
+function sort() {
+    let persons = [{
+            name: "Иван",
+            age: 17
+        },
+        {
+            name: "Мария",
+            age: 35
+        },
+        {
+            name: "Алексей",
+            age: 73
+        },
+        {
+            name: "Яков",
+            age: 12
+        },
+    ]
+
+    function sort(field, boolean = true) {
+        if (boolean) {
+            return (a, b) => a[field] > b[field] ? 1 : -1
+        } else {
+            return (a, b) => a[field] < b[field] ? 1 : -1
+        }
+    }
+
+    console.log(persons.sort(sort('age', false)))
+}
+// sort()
+
+
+function arrayMap() {
+    let arr = ["1", {}, null, undefined, "500", 700];
+    let newArr = arr.map(item => typeof (item) === "string" ? parseInt(item) : item);
+    console.log(newArr)
+}
+// arrayMap()
+
+
+function arrayReduce() {
+    let arr = ['0', 5, 3, "string", null];
+
+    let result = arr.reduce((sum, currentItem) => {
+        if (typeof (currentItem) === "number") {
+            sum *= currentItem;
+        }
+        return sum
+
+    }, 1)
+    console.log(result);
+}
+// arrayReduce()
+
+
+function objectFilter() {
+    let phone = {
+        brand: "meizu",
+        model: "m2",
+        ram: 2,
+        color: "black",
+    };
+
+    function filter(obj, func) {
+        for (let key in obj) {
+            console.log(func)
+            if (!func(key, obj[key])) {
+                delete obj[key]
+            }
+        }
+    }
+
+    filter(phone, (key, value) => key == "color" || value == 2);
+    console.log(phone)
+}
+// objectFilter()
+
+//OR
+
+// let newObj = Object.entries(phone)
+//     .filter(item => item[0] === "color" || item[0] === "ram")
+//     .map(item => item);
+// console.log(newObj)
+
+
+function objectMap() {
+    function map(object, callback) {
+        let obj = {};
+        for (let key in object) {
+            Object.assign(obj, callback(key, object[key]));
+        }
+        return obj
+    }
+
+    let obj = map({
+        name: "Иван",
+        age: 17
+    }, function (key, value) {
+        let result = {};
+        result[key + "_"] = value + "$";
+        return result;
+    })
+    console.log(obj);
+}
+// objectMap()
+
+
+function recursion() {
+    let sum = (firstNumber, step, n) => {
+        return n == 1 ? firstNumber : firstNumber + sum(firstNumber + step, step, n - 1);
+    }
+}
+// recursion()
+
+//TODO: HTML Tree (in process...)