Browse Source

Fourth commit - objects, json

Bonyant 2 years ago
parent
commit
56aff6d918
3 changed files with 267 additions and 0 deletions
  1. 12 0
      04/index.html
  2. 223 0
      04/script.js
  3. 32 0
      04/styles.css

+ 12 - 0
04/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" />
+    <script src="script.js"></script>
+    <link rel="stylesheet" href="styles.css" />
+    <title>JavaScript</title>
+  </head>
+  <body></body>
+</html>

+ 223 - 0
04/script.js

@@ -0,0 +1,223 @@
+// const array = [];
+// for (let i = 0; i < 10; i++) {
+//   array.push(i);
+// }
+// for (let i = 1; i < array.length; i++) {
+//   array[i] = array[i] + array[i - 1];
+// }
+// console.log(array);
+
+// 3 persons
+
+// const a = {
+//   name: "Anatoliy",
+//   surname: "Stateham",
+// };
+// const b = {
+//   name: "Arnold",
+//   surname: "Hudoba",
+// };
+// const c = {
+//   name: "Stepan",
+//   surname: "Johnson",
+// };
+
+// different fields
+
+// a.age = 16;
+// a.fathername = "Andreevich";
+// a.sex = "male";
+// b.age = 25;
+// b.fathername = "Bogdanovich";
+// b.sex = "male";
+// c.age = 18;
+// c.fathername = "Vasilevich";
+// c.sex = "male";
+
+// fields check
+
+// if ("age" in a) {
+//   alert("Optional field 'age' found in object a");
+// }
+// if ("fathername" in a) {
+//   alert("Optional field 'fathername' found in object a");
+// }
+// if ("sex" in a) {
+//   alert("Optional field 'sex' found in object a");
+// }
+// if ("age" in b) {
+//   alert("Optional field 'age' found in object b");
+// }
+// if ("fathername" in b) {
+//   alert("Optional field 'fathername' found in object b");
+// }
+// if ("sex" in b) {
+//   alert("Optional field 'sex' found in object b");
+// }
+// if ("age" in c) {
+//   alert("Optional field 'age' found in object c");
+// }
+// if ("fathername" in c) {
+//   alert("Optional field 'fathername' found in object c");
+// }
+// if ("sex" in c) {
+//   alert("Optional field 'sex' found in object c");
+// }
+
+// array of persons
+
+// const persons = [];
+// persons.push(a, b, c);
+// persons.push({
+//   name: "Semen",
+//   surname: "Harrington",
+//   age: 30,
+//   fathername: "Sergeevich",
+//   sex: "male",
+// });
+
+// loop of persons
+
+// for (let person in persons) {
+//   console.log(person);
+// }
+
+// loop of name and surname
+
+// for (let person in persons) {
+//   console.log(person.name, person.surname);
+// }
+
+// loop of loop of values
+
+// let count = 0;
+// for (let person in persons) {
+//   console.log(`--- ${++count} object ---`);
+//   for (key in persons[person]) {
+//     console.log(`${key}: ${persons[person][key]}`);
+//   }
+// }
+
+// fullName
+
+// for (let person in persons) {
+//   for (key in persons[person]) {
+//     if (!persons[person]?.fathername) {
+//       persons[
+//         person
+//       ].fullName = `${persons[person].name} ${persons[person].surname}`;
+//     } else {
+//       persons[
+//         person
+//       ].fullName = `${persons[person].name} ${persons[person].surname} ${persons[person].fathername}`;
+//     }
+//   }
+// }
+// console.log(persons);
+
+// serialize
+
+// const stringPersons = JSON.stringify(persons);
+// console.log(stringPersons);
+
+// deserialize
+
+// let personString =
+//   '{"name":"Mariya","surname":"Gordon","age":26,"sex":"female","fullName":"Mariya Gordon"}';
+// persons.push(JSON.parse(personString));
+// console.log(persons);
+
+// HTML && HTML optional fields && HTML tr color && HTML th optional
+
+// let countHTML = 0;
+// for (let person in persons) {
+//   let strObj = `<table border='1' class='obj${++countHTML}' style='margin: 1vh'>`;
+//   strObj += `<caption>${countHTML} object in persons</caption>`;
+//   for (let [key, value] of Object.entries(persons[person])) {
+//     strObj += "<tr>";
+//     strObj += "<td>";
+//     strObj += key[0].toUpperCase() + key.slice(1);
+//     strObj += "</td>";
+//     strObj += "<td>";
+//     strObj += value;
+//     strObj += "</td>";
+//     strObj += "</tr>";
+//   }
+//   strObj += "</table>";
+//   document.write(strObj);
+// }
+
+// Задание на синий пояс.
+
+// const someTree = {
+//   tagName: "table", //html tag
+//   subTags: [
+//     //вложенные тэги
+//     {
+//       tagName: "tr",
+//       subTags: [
+//         {
+//           tagName: "td",
+//           text: "some text",
+//         },
+//         {
+//           tagName: "td",
+//           text: "some text 2",
+//         },
+//       ],
+//     },
+//   ],
+//   attrs: {
+//     border: 1,
+//   },
+// };
+
+// let constructor = "";
+// let countTables = 0;
+
+// constructor += `<${someTree.tagName} ${[...Object.keys(someTree.attrs)]}='${
+//   someTree.attrs.border
+// }' class='obj${++countTables}' style='margin: 1vh;'>`;
+// constructor += `<${someTree.subTags[0].tagName}>`;
+// // -----
+// constructor += `<${someTree.subTags[0].subTags[0].tagName}>`;
+// constructor += `${someTree.subTags[0].subTags[0].text}`;
+// constructor += `</${someTree.subTags[0].subTags[0].tagName}>`;
+// // -----
+// constructor += `<${someTree.subTags[0].subTags[1].tagName}>`;
+// constructor += `${someTree.subTags[0].subTags[1].text}`;
+// constructor += `</${someTree.subTags[0].subTags[1].tagName}>`;
+// // -----
+// constructor += `</${someTree.subTags[0].tagName}>`;
+// constructor += `</${someTree.tagName}>`;
+
+// document.write(constructor); // данный конструктор не гибкий, работает пока-что только для заданного варианта, в дальнейшем, сделаю конструктор гибким
+
+// destruct array
+
+// let arr = [1, 2, 3, 4, 5, "a", "b", "c"];
+// let [odd1, even1, odd2, even2, odd3, ...letters] = arr;
+// console.log(odd1, even1, odd2, even2, odd3, letters);
+
+// destruct string
+
+// let arr = [1, "abc"];
+// let [number, [...[s1, s2, s3]]] = arr;
+// console.log(number, s1, s2, s3);
+
+// destruct 2
+
+// let obj = {
+//   name: "Ivan",
+//   surname: "Petrov",
+//   children: [{ name: "Maria" }, { name: "Nikolay" }],
+// };
+// let { name: nameObj, sursname: surnameObj, children: childrenNames } = obj;
+// let [{ name: name1 }, { name: name2 }] = childrenNames;
+// console.log(name1, name2);
+
+// destruct 3
+
+// let arr = [1, 2, 3, 4, 5, 6, 7, 10];
+// let { 0: a, 1: b, length } = arr;
+// console.log(a, b, length);

+ 32 - 0
04/styles.css

@@ -0,0 +1,32 @@
+body {
+  background-color: #230b36;
+  font-family: "Lucida Sans Unicode", "Lucida Grande", Sans-Serif;
+  display: flex;
+  justify-content: center;
+}
+table {
+  border-collapse: collapse;
+  color: #000000;
+}
+caption {
+  padding: 10px;
+  color: white;
+  background: #8fd4c1;
+  font-size: 18px;
+  text-align: left;
+  font-weight: bold;
+}
+th {
+  border-bottom: 3px solid #b9b29f;
+  padding: 10px;
+  text-align: left;
+}
+td {
+  padding: 10px;
+}
+tr:nth-child(odd) {
+  background: white;
+}
+tr:nth-child(even) {
+  background: #e8e6d1;
+}