|
@@ -0,0 +1,210 @@
|
|
|
|
+// sort--------------------------------------------------------------------------------------------------------------------------;
|
|
|
|
+
|
|
|
|
+var persons = [
|
|
|
|
+ { name: "Иван", age: 17 },
|
|
|
|
+ { name: "Мария", age: 35 },
|
|
|
|
+ { name: "Алексей", age: 73 },
|
|
|
|
+ { name: "Яков", age: 12 },
|
|
|
|
+];
|
|
|
|
+
|
|
|
|
+function byField(field, order) {
|
|
|
|
+ var collator = new Intl.Collator(["en", "ru"], {
|
|
|
|
+ numeric: true,
|
|
|
|
+ });
|
|
|
|
+ order = order ? 1 : -1;
|
|
|
|
+ return function (a, b) {
|
|
|
|
+ return order * collator.compare(a[field], b[field]);
|
|
|
|
+ };
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+persons.sort(byField("age"));
|
|
|
|
+persons.sort(byField("name", false));
|
|
|
|
+
|
|
|
|
+// array map----------------------------------------------------------------------------------------------------------------------;
|
|
|
|
+{
|
|
|
|
+ let prro = ["1", {}, null, undefined, "500", 700];
|
|
|
|
+
|
|
|
|
+ let result = prro.map(function (item, index, arr) {
|
|
|
|
+ var number = parseInt(item);
|
|
|
|
+ return isNaN(number) ? item : number;
|
|
|
|
+ });
|
|
|
|
+ console.log(result);
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+// array reduce-------------------------------------------------------------------------------------------------------------------;
|
|
|
|
+{
|
|
|
|
+ let arrReduce = ["0", 5, 3, "string", null];
|
|
|
|
+
|
|
|
|
+ function numb(sum, i) {
|
|
|
|
+ if (!isNaN(i) && isFinite(i) && i != 0 && i != null) {
|
|
|
|
+ return (sum *= i);
|
|
|
|
+ }
|
|
|
|
+ return sum;
|
|
|
|
+ }
|
|
|
|
+ var result = arrReduce.reduce(numb, 1);
|
|
|
|
+
|
|
|
|
+ console.log(result);
|
|
|
|
+}
|
|
|
|
+// object filter-------------------------------------------------------------------------------------------------------------------;
|
|
|
|
+{
|
|
|
|
+ let laptop = {
|
|
|
|
+ brand: "Lenovo",
|
|
|
|
+ model: "Legion",
|
|
|
|
+ RAM: "16 Gb",
|
|
|
|
+ ROM: "512 Gb",
|
|
|
|
+ processorModel: "Ryzen_5",
|
|
|
|
+ numberOfCores: 6,
|
|
|
|
+ };
|
|
|
|
+ function filter(a, b, c) {
|
|
|
|
+ for (var key in a) {
|
|
|
|
+ if (key[0] === b) {
|
|
|
|
+ delete a[key];
|
|
|
|
+ } else if (a[key] === c) {
|
|
|
|
+ delete a[key];
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ return a;
|
|
|
|
+ }
|
|
|
|
+ console.log(filter(laptop, "b", 6));
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+// object map-------------------------------------------------------------------------------------------------------------------;
|
|
|
|
+
|
|
|
|
+function map(obj, funct) {
|
|
|
|
+ for (key in obj) {
|
|
|
|
+ funct(obj, key, obj[key]);
|
|
|
|
+ delete obj[key];
|
|
|
|
+ }
|
|
|
|
+ return obj;
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+function newObj(obj, key, value) {
|
|
|
|
+ obj[key + "^*&"] = value + "^^";
|
|
|
|
+ return obj;
|
|
|
|
+}
|
|
|
|
+console.log(
|
|
|
|
+ map({ brand: "Mercedes", model: "Vito", volume: 2.2, maxSpeed: 200 }, newObj)
|
|
|
|
+);
|
|
|
|
+
|
|
|
|
+// Рекурсия-------------------------------------------------------------------------------------------------------------------;
|
|
|
|
+// Sum-------------------------------------------------------------------------------------------------------------------;
|
|
|
|
+function sum(n) {
|
|
|
|
+ if (n == 1) return 1;
|
|
|
|
+ return n + sumTo(n - 1);
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+console.log(sum(55));
|
|
|
|
+
|
|
|
|
+// HTML Tree-------------------------------------------------------------------------------------------------------------------;
|
|
|
|
+
|
|
|
|
+let body = {
|
|
|
|
+ tagName: "body",
|
|
|
|
+ subTags: [
|
|
|
|
+ {
|
|
|
|
+ tagName: "div",
|
|
|
|
+ subTags: [
|
|
|
|
+ {
|
|
|
|
+ tagName: "span",
|
|
|
|
+ text: "Enter a data please:",
|
|
|
|
+ },
|
|
|
|
+ {
|
|
|
|
+ tagName: "br",
|
|
|
|
+ },
|
|
|
|
+ {
|
|
|
|
+ tagName: "input",
|
|
|
|
+ attrs: {
|
|
|
|
+ type: "text",
|
|
|
|
+ id: "name",
|
|
|
|
+ },
|
|
|
|
+ },
|
|
|
|
+ {
|
|
|
|
+ tagName: "div",
|
|
|
|
+ subTags: [
|
|
|
|
+ {
|
|
|
|
+ tagName: "input",
|
|
|
|
+ attrs: {
|
|
|
|
+ type: "text",
|
|
|
|
+ id: "surname",
|
|
|
|
+ },
|
|
|
|
+ },
|
|
|
|
+ {
|
|
|
|
+ tagName: "div",
|
|
|
|
+ subTags: [
|
|
|
|
+ {
|
|
|
|
+ tagName: "button",
|
|
|
|
+ text: "Some text",
|
|
|
|
+ attrs: {
|
|
|
|
+ id: "newBtn",
|
|
|
|
+ },
|
|
|
|
+ },
|
|
|
|
+ {
|
|
|
|
+ tagName: "div",
|
|
|
|
+ subTags: [
|
|
|
|
+ {
|
|
|
|
+ tagName: "div",
|
|
|
|
+ subTags: [
|
|
|
|
+ {
|
|
|
|
+ tagName: "div",
|
|
|
|
+ subTags: [
|
|
|
|
+ {
|
|
|
|
+ tagName: "input",
|
|
|
|
+ attrs: {
|
|
|
|
+ type: "date",
|
|
|
|
+ id: "newDate",
|
|
|
|
+ },
|
|
|
|
+ },
|
|
|
|
+ ],
|
|
|
|
+ },
|
|
|
|
+ ],
|
|
|
|
+ },
|
|
|
|
+ ],
|
|
|
|
+ },
|
|
|
|
+ ],
|
|
|
|
+ },
|
|
|
|
+ ],
|
|
|
|
+ },
|
|
|
|
+ ],
|
|
|
|
+ },
|
|
|
|
+ {
|
|
|
|
+ tagName: "div",
|
|
|
|
+ subTags: [
|
|
|
|
+ {
|
|
|
|
+ tagName: "button",
|
|
|
|
+ text: "OK",
|
|
|
|
+ attrs: {
|
|
|
|
+ id: "ok",
|
|
|
|
+ },
|
|
|
|
+ },
|
|
|
|
+ {
|
|
|
|
+ tagName: "button",
|
|
|
|
+ text: "cancel",
|
|
|
|
+ attrs: {
|
|
|
|
+ id: "cancel",
|
|
|
|
+ },
|
|
|
|
+ },
|
|
|
|
+ ],
|
|
|
|
+ },
|
|
|
|
+ ],
|
|
|
|
+};
|
|
|
|
+
|
|
|
|
+function createElem(tagName, attrs, text) {
|
|
|
|
+ var elem = document.createElement(tagName);
|
|
|
|
+ if (attrs) for (var name in attrs) elem.setAttribute(name, attrs[name]);
|
|
|
|
+ if (text) elem.textContent = text;
|
|
|
|
+ return elem;
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+function walker(tree) {
|
|
|
|
+ var tagName = tree.tagName,
|
|
|
|
+ attrs = tree.attrs,
|
|
|
|
+ text = tree.text,
|
|
|
|
+ elem = createElem(tagName, attrs, text);
|
|
|
|
+ if (tree.subTags)
|
|
|
|
+ tree.subTags.forEach(function (el) {
|
|
|
|
+ el = walker(el);
|
|
|
|
+ elem.appendChild(el);
|
|
|
|
+ });
|
|
|
|
+ return elem;
|
|
|
|
+}
|
|
|
|
+var table = walker(body);
|
|
|
|
+document.body.appendChild(table);
|