Browse Source

HW<8> done

Levshin95 1 year ago
parent
commit
05c403c0ca
2 changed files with 80 additions and 0 deletions
  1. 12 0
      HW8/index.html
  2. 68 0
      HW8/index.js

+ 12 - 0
HW8/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="index.js"></script>
+</body>
+</html>

+ 68 - 0
HW8/index.js

@@ -0,0 +1,68 @@
+/* Сортировка */
+
+var persons = [
+    {name: "Иван", age: 17},
+    {name: "Мария", age: 35},
+    {name: "Алексей", age: 73},
+    {name: "Яков", age: 12},
+]
+
+function sort(array, field, order = true) {
+    let arrSorted = array.sort(function (a, b){
+        if (a[field] > b[field]) {
+            return order ? 1 : -1;
+        }
+        else{
+            return order ? -1 : 1;
+        }
+    })
+    return arrSorted
+
+}
+
+sort(persons, 'age', false)
+
+/* array map */
+
+let arr = ["1", {}, null, undefined, "500", 700]
+let arr1 = arr.map(x => typeof x === 'string' ? +x : x)
+console.log(arr1)
+
+/* array reduce */
+
+let arr2 = ["0", 5, 3, "string", null];
+let arr3 = arr2.filter(x => typeof x === 'number').reduce((a, b) => a *= b)
+console.log(arr3)
+
+/* object filter */
+
+var phone = {
+    brand: "meizu",
+    model: "m2",
+    ram: 2,
+    color: "black",
+};
+filter(phone, (key, value) => key == "color" || value == 2);
+
+function filter(obj, key, value) {
+    let object = {}
+        for(let key in obj) {
+            for(let value in obj) {
+                if (obj[key] == 2 && value == "color") {
+                    object[key] = obj[key]
+                    object[value] = obj[value]
+                }
+            }
+        }
+        return object
+    }
+
+/* Рекурсия: sum */
+
+function progression(n) {
+    if (n === 0) {
+        return 0
+    } else {
+        return n + progression(n - 1)
+    }
+}