|
@@ -0,0 +1,197 @@
|
|
|
+// Task sort
|
|
|
+// let persons = [
|
|
|
+// {name: "Иван", age: 17},
|
|
|
+// {name: "Мария", age: 35},
|
|
|
+// {name: "Алексей", age: 73},
|
|
|
+// {name: "Яков", age: 12},
|
|
|
+// ];
|
|
|
+
|
|
|
+// function sort (arr, key, boolean = true) {
|
|
|
+// for (let i = 0; i < (arr.length - 1); i++) {
|
|
|
+// for (let q = 0; q < arr.length - 1; q++) {
|
|
|
+// if (arr[q][key] > arr[q + 1][key]) {
|
|
|
+// let b = arr[q];
|
|
|
+// arr[q] = arr[q + 1];
|
|
|
+// arr[q + 1] = b;
|
|
|
+// };
|
|
|
+// if ((arr[q][key] < arr[q + 1][key]) && !boolean ) {
|
|
|
+// let b = arr[q];
|
|
|
+// arr[q] = arr[q + 1];
|
|
|
+// arr[q +1] = b;
|
|
|
+// };
|
|
|
+// };
|
|
|
+// };console.log(arr);
|
|
|
+// return arr;
|
|
|
+// };
|
|
|
+
|
|
|
+// sort(persons, 'name', false);
|
|
|
+
|
|
|
+
|
|
|
+//Task array map
|
|
|
+
|
|
|
+// let myArray = ["1", {}, null, undefined, "500", 700];
|
|
|
+
|
|
|
+// function changStringToNumber (arr) {
|
|
|
+// for (let i = 0; i < arr.length; i++) {
|
|
|
+// if (typeof (arr[i]) === 'string') {
|
|
|
+// arr[i] = +arr[i];
|
|
|
+// };
|
|
|
+// };
|
|
|
+// console.log(arr);
|
|
|
+// return arr;
|
|
|
+// };
|
|
|
+
|
|
|
+// changStringToNumber(myArray);
|
|
|
+//2
|
|
|
+// let myArray = ["1", {}, null, undefined, "500", 700];
|
|
|
+// let newArray = myArray.map( function (value) {
|
|
|
+// if (typeof(value) === 'string') {
|
|
|
+// value = +value;
|
|
|
+// return value;
|
|
|
+// };
|
|
|
+// return value;
|
|
|
+// });
|
|
|
+// console.log(newArray);
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+//Task array reduce
|
|
|
+
|
|
|
+// let myArray = ["0", 5, 3, "string", null];
|
|
|
+
|
|
|
+// function multiplicationStrings (arr) {
|
|
|
+// let result = 1;
|
|
|
+// for (let i = 0; i < arr.length; i++) {
|
|
|
+// if (typeof (arr[i]) === 'number') {
|
|
|
+// result *= (+arr[i]);
|
|
|
+// };
|
|
|
+// };
|
|
|
+// console.log(result);
|
|
|
+// return result;
|
|
|
+// };
|
|
|
+
|
|
|
+// multiplicationStrings(myArray);
|
|
|
+//2
|
|
|
+
|
|
|
+// let myArray = ["0", 5, 3, "string", null];
|
|
|
+// let result = myArray. reduce(function multiplicationStrings (acum, curentVal) {
|
|
|
+// if (typeof(curentVal) === 'number') {
|
|
|
+// return (acum * curentVal);
|
|
|
+// };
|
|
|
+// return acum;
|
|
|
+// }, 1);
|
|
|
+// console.log(result);
|
|
|
+
|
|
|
+
|
|
|
+//Task object filter
|
|
|
+
|
|
|
+// var phone = {
|
|
|
+// brand: "meizu",
|
|
|
+// model: "m2",
|
|
|
+// ram: 2,
|
|
|
+// color: "black",
|
|
|
+// };
|
|
|
+
|
|
|
+// function myFilter (obj, func) {
|
|
|
+// let newObj = {};
|
|
|
+// for (let key in obj) {
|
|
|
+// let checkArg = func (key, value =obj[key]);
|
|
|
+// if (checkArg) {
|
|
|
+// newObj[key] = value;
|
|
|
+// };
|
|
|
+// };
|
|
|
+// return newObj;
|
|
|
+// };
|
|
|
+
|
|
|
+
|
|
|
+// let check = myFilter(phone,(key,value) => key == "color" || value == 2);
|
|
|
+// console.log (check);
|
|
|
+
|
|
|
+
|
|
|
+//Task object map
|
|
|
+
|
|
|
+// function myMap (obj, func) {
|
|
|
+// let newObj = {};
|
|
|
+// for (let key in obj) {
|
|
|
+// let checkArg = func (key, value =obj[key]);
|
|
|
+// Object.assign(newObj,checkArg);
|
|
|
+// };
|
|
|
+// return newObj;
|
|
|
+// };
|
|
|
+// let check = myMap({name: "Иван", age: 17},function(key,value){
|
|
|
+// var result = {};
|
|
|
+// result[key+"_"] = value + "$";
|
|
|
+// return result;
|
|
|
+// }); //должен вернуть {name_: "Иван$", age_: "17$"}
|
|
|
+
|
|
|
+// console.log(check);
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+// Task Sum
|
|
|
+
|
|
|
+// function calculationSumArithmProgr (a, d, n) {
|
|
|
+
|
|
|
+// if (n === 1) {
|
|
|
+// return a;
|
|
|
+// };
|
|
|
+
|
|
|
+// return (calculationSumArithmProgr(a, d, n-1) + (a + d * (n - 1)));
|
|
|
+// };
|
|
|
+
|
|
|
+// let check = calculationSumArithmProgr(5, 7, 3);
|
|
|
+// console.log(check);
|
|
|
+
|
|
|
+
|
|
|
+//Task 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 builder (obj) {
|
|
|
+ let {tagName, attrs, text, children} = obj;
|
|
|
+
|
|
|
+ let attributes = '';
|
|
|
+
|
|
|
+ if (attrs) {
|
|
|
+ for (const [key, value] of Object.entries(attrs)) {
|
|
|
+ attributes += `${key} = ${value}` ;
|
|
|
+ };
|
|
|
+ };
|
|
|
+
|
|
|
+ if (!children) {
|
|
|
+ return `<${tagName} ${attributes}>${text}</${tagName}>`;
|
|
|
+ };
|
|
|
+
|
|
|
+ let siblings = '';
|
|
|
+
|
|
|
+ for (let i = 0; i < children.length; i++) {
|
|
|
+ siblings += `${builder(children[i])}\n`;
|
|
|
+ };
|
|
|
+
|
|
|
+ return `<${tagName} ${attributes}>\n ${siblings}</${tagName}>\n`;
|
|
|
+};
|
|
|
+
|
|
|
+let check = builder(someTree);
|
|
|
+
|
|
|
+document.write(check);
|