Browse Source

homework8 done

holevchuk.evgeny 1 year ago
parent
commit
cf9e829951
8 changed files with 161 additions and 7 deletions
  1. 9 7
      hw03_part2/2_switch_if.js
  2. 9 0
      hw08/array_map.js
  3. 9 0
      hw08/array_reduce.js
  4. 65 0
      hw08/html_tree.js
  5. 26 0
      hw08/object_filter.js
  6. 20 0
      hw08/object_map.js
  7. 3 0
      hw08/recursively_sum.js
  8. 20 0
      hw08/sort.js

+ 9 - 7
hw03_part2/2_switch_if.js

@@ -2,14 +2,16 @@ let color = prompt("Введите цвет","");
 
 if(color === 'red') {
 	document.write("<div style='background-color: red;'>красный</div>");
+}
+if(color === 'black' || color === 'red') {
 	document.write("<div style='background-color: black; color: white;'>черный</div>");
-} else if(color === 'black') {
-	document.write("<div style='background-color: black; color: white;'>черный</div>");
-} else if(color === 'blue') {
+}
+if(color === 'blue') {
 	document.write("<div style='background-color: blue;'>синий</div>");
-	document.write("<div style='background-color: green;'>зеленый</div>");
-} else if(color === 'green') {
-	document.write("<div style='background-color: green;'>зеленый</div>");
-} else {
+}
+if(color === 'green' || color === 'blue') {
+	document.write("<div style='background-color: green; color: white;'>зеленый</div>");
+}
+if(!color) {
 	document.write("<div style='background-color: gray;'>Я не понял</div>");
 }

+ 9 - 0
hw08/array_map.js

@@ -0,0 +1,9 @@
+let arr = ["1", {}, null, undefined, "500", 700];
+
+arr.map((elem, index, arr) => {
+	if(typeof elem === 'string') {
+		arr[index] = +elem;
+	}
+});
+
+console.log(arr); // [ 1, {}, null, undefined, 500, 700 ]

+ 9 - 0
hw08/array_reduce.js

@@ -0,0 +1,9 @@
+const arr = ["0", 5, 3, "string", null];
+
+const sum = params => {
+	const multiply = (previousValue, currentValue) => (typeof currentValue === 'number' ? previousValue * currentValue : previousValue);
+
+	return params.reduce(multiply, 1);
+}
+
+console.log(sum(arr)); // 15

+ 65 - 0
hw08/html_tree.js

@@ -0,0 +1,65 @@
+let someTree = {
+	tagName: "table", //html tag
+	children: [ //вложенные тэги
+		{
+			tagName: "tr",
+			children: [
+				{
+					tagName: "td",
+					text: "some text 11",
+					children: [
+						{
+							tagName: "span",
+							text: "some text 222",
+						}
+					]
+				},
+				{
+					tagName: "td",
+					text: "some text 33",
+				}
+			]
+		},
+		{
+			tagName: "tr",
+			children: [
+				{
+					tagName: "td",
+					text: "some text 44",
+					children: [
+						{
+							tagName: "span",
+							text: "some text 555",
+						}
+					]
+				},
+				{
+					tagName: "td",
+					text: "some text 66",
+				}
+			]
+		}
+	],
+	attrs:
+		{
+			border: 1,
+		},
+}
+
+let resultStr = '';
+const objectWalker = (object) => {
+
+	for (const attr in object.attrs) {
+		resultStr += `<${object.tagName} ${attr}="${object.attrs[attr]}">`;
+	}
+	if(object.children) {
+		for (let child of object.children) {
+			resultStr += `<${child.tagName}>${child.text ? child.text : ''}`;
+			objectWalker(child);
+		}
+	}
+
+	return resultStr += `</${object.tagName}>`;
+}
+
+console.log(objectWalker(someTree)); // <table border="1"><tr><td>some text 11<span>some text 222</span></td><td>some text 33</td></tr><tr><td>some text 44<span>some text 555</span></td><td>some text 66</td></tr></table>

+ 26 - 0
hw08/object_filter.js

@@ -0,0 +1,26 @@
+let phone = {
+	brand: "meizu",
+	model: "m2",
+	ram: 2,
+	color: "black",
+};
+
+const filter1 = (obj, callback) => {
+	return Object.fromEntries(Object.entries(obj).filter(([key, val]) => callback(key, val)));
+}
+
+// или
+
+const filter2 = (obj, condition) => {
+	const filteredObj = {};
+
+	for (const [key, value] of Object.entries(obj)) {
+		if(condition(key, value)) {
+			filteredObj[key] = obj[key];
+		}
+	}
+	return filteredObj;
+}
+
+console.log(filter1(phone,(key,value) => key === "color" || value === 2)); // { ram: 2, color: 'black' }
+console.log(filter2(phone,(key,value) => key === "color" || value === 2)); // { ram: 2, color: 'black' }

+ 20 - 0
hw08/object_map.js

@@ -0,0 +1,20 @@
+let obj = {
+	name: "Иван",
+	age: 17
+}
+
+const map = (obj, func) => {
+	let newObj = {};
+	for (const [key, value] of Object.entries(obj)) {
+		obj = Object.assign(newObj, func(key, value));
+	}
+	return obj;
+}
+
+const mappedObject = map(obj,(key,value) =>{
+	let result = {};
+	result[key+"_"] = value + "$";
+	return result;
+});
+
+console.log(mappedObject); // { name_: 'Иван$', age_: '17$' }

+ 3 - 0
hw08/recursively_sum.js

@@ -0,0 +1,3 @@
+const sum = number => number > 0 ? number + sum(number - 1) : number;
+
+console.log(sum(5)); // 15

+ 20 - 0
hw08/sort.js

@@ -0,0 +1,20 @@
+let persons = [
+	{name: "Иван", age: 17},
+	{name: "Мария", age: 35},
+	{name: "Алексей", age: 73},
+	{name: "Яков", age: 12},
+]
+
+const mySort = (arr, sortField, ascDesc = true) => {
+	const sortFunc = (a, b) => {
+		if (a[sortField] < b[sortField]){
+			return ascDesc ? -1 : 1;
+		}
+		return ascDesc ? 1 : -1;
+	}
+
+	return arr.sort(sortFunc);
+};
+
+console.log(mySort(persons, 'age'));
+console.log(mySort(persons, 'name', false));