|
@@ -0,0 +1,210 @@
|
|
|
+//3 persons
|
|
|
+//different fields
|
|
|
+//fields check
|
|
|
+let a = {
|
|
|
+ personName: "Vasya",
|
|
|
+ surname: "Pupkin",
|
|
|
+ age: 11
|
|
|
+};
|
|
|
+let b = {
|
|
|
+ personName: "Ivan",
|
|
|
+ surname: "Ivanov",
|
|
|
+ secondName: "Ivanovich"
|
|
|
+};
|
|
|
+let c = {
|
|
|
+ personName: "Kiril",
|
|
|
+ surname: "Kirilovich",
|
|
|
+ sex: "male"
|
|
|
+
|
|
|
+}
|
|
|
+
|
|
|
+let arrayOptionalFields = ["age", "secondName", "sex"];
|
|
|
+
|
|
|
+let getOptionalFields = function(arr) {
|
|
|
+ for(let item of arrayOptionalFields) {
|
|
|
+ if(item in arr) {
|
|
|
+ alert(arr[item]);
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+getOptionalFields(a);
|
|
|
+getOptionalFields(b);
|
|
|
+getOptionalFields(c);
|
|
|
+
|
|
|
+
|
|
|
+//array of persons
|
|
|
+//loop of persons
|
|
|
+//loop of name and surname
|
|
|
+let persons = [{
|
|
|
+ personName: "Vlad",
|
|
|
+ surname: "Vladlen",
|
|
|
+ relationship: false
|
|
|
+}];
|
|
|
+
|
|
|
+persons.push(a, b, c);
|
|
|
+
|
|
|
+for(let person of persons) {
|
|
|
+ console.log(person.name);
|
|
|
+ console.log(person.surname);
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+//loop of loop of values
|
|
|
+for(let person of persons) {
|
|
|
+ let keysArr = Object.keys(person);
|
|
|
+
|
|
|
+ for(let i = 0; i < keysArr.length; i++) {
|
|
|
+ console.log(person[keysArr[i]]);
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+//fullName
|
|
|
+for(let person of persons) {
|
|
|
+ person["fullName"] = `${person.personName} ${person.surname}`;
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+//serialize
|
|
|
+let personsJSON = JSON.stringify(persons);
|
|
|
+
|
|
|
+
|
|
|
+//deserialize
|
|
|
+let hodorPerson = {
|
|
|
+ personName: "Hodor",
|
|
|
+ surname: "Hodor",
|
|
|
+ favoritWord: "Hodor"
|
|
|
+}
|
|
|
+
|
|
|
+let hodorPersonJSON = JSON.stringify(hodorPerson);
|
|
|
+
|
|
|
+persons.push(JSON.parse(hodorPersonJSON));
|
|
|
+
|
|
|
+
|
|
|
+//HTML
|
|
|
+//HTML optional fields
|
|
|
+//HTML tr color
|
|
|
+//HTML th optional
|
|
|
+let allPersonsKey = []; //В этой переменной я буду хранить все ключи которые есть в объектах person
|
|
|
+let uniquePersonsKey = []; //Тут хранятся все уникальные ключи
|
|
|
+let counter = 0; //Это переменная быдет служить проверкой на четность и нечетность, с помощью этой проверки я буду красить строки через один
|
|
|
+
|
|
|
+for(let person of persons) { //Весь этот цыкл отвечает за создание массива с уникальными ключами
|
|
|
+ let personKeyArr = Object.keys(person);
|
|
|
+ for(let item of personKeyArr) {
|
|
|
+ allPersonsKey.push(item);
|
|
|
+ }
|
|
|
+ uniquePersonsKey = [...new Set(allPersonsKey)];
|
|
|
+}
|
|
|
+
|
|
|
+let str = "<table>"; //Начинаю рисовать таблицу
|
|
|
+
|
|
|
+str += "<tr>";
|
|
|
+for(let i = 0; i < uniquePersonsKey.length; i++) { //Рисую шапку таблицы перечисляя все уникальные ключи
|
|
|
+ str += `<th>${uniquePersonsKey[i]}</th>`;
|
|
|
+}
|
|
|
+str += "</tr>";
|
|
|
+
|
|
|
+for(let person of persons) { //Отрисовка тела таблицы
|
|
|
+ let backgroundColor;
|
|
|
+ counter++;
|
|
|
+
|
|
|
+ if(counter%2) { //Тут идет проверка, четный ход или не четный
|
|
|
+ backgroundColor = "yellow";
|
|
|
+ } else {
|
|
|
+ backgroundColor = "green";
|
|
|
+ }
|
|
|
+
|
|
|
+ str += `<tr style="background-color: ${backgroundColor}">`;
|
|
|
+ for(let i = 0; i < uniquePersonsKey.length; i++) {
|
|
|
+ str += `<td>${person[uniquePersonsKey[i]]}</td>`;
|
|
|
+ }
|
|
|
+ str += "</tr>";
|
|
|
+}
|
|
|
+
|
|
|
+str += "</table>";
|
|
|
+
|
|
|
+document.write(str);
|
|
|
+
|
|
|
+
|
|
|
+//Задание на синий пояс.
|
|
|
+var someTree = {
|
|
|
+ tagName: "table", //html tag
|
|
|
+ subTags: [ //вложенные тэги
|
|
|
+ {
|
|
|
+ tagName: "tr",
|
|
|
+ subTags: [
|
|
|
+ {
|
|
|
+ tagName: "td",
|
|
|
+ text: "some text",
|
|
|
+ },
|
|
|
+ {
|
|
|
+ tagName: "td",
|
|
|
+ text: "some text 2",
|
|
|
+ }
|
|
|
+ ]
|
|
|
+ }
|
|
|
+ ],
|
|
|
+ attrs:
|
|
|
+ {
|
|
|
+ border: 1,
|
|
|
+ },
|
|
|
+}
|
|
|
+
|
|
|
+let str1 = `<${someTree.tagName}>`;
|
|
|
+
|
|
|
+for(let i = 0; i < someTree.subTags.length; i++) {
|
|
|
+ str1 += `<${someTree.subTags[i].tagName}>`;
|
|
|
+
|
|
|
+ for(let j = 0; j < someTree.subTags[i].subTags.length; j++) {
|
|
|
+ str1 += `<${someTree.subTags[i].subTags[j].tagName}>${someTree.subTags[i].subTags[j].text}</${someTree.subTags[i].subTags[j].tagName}>`;
|
|
|
+ }
|
|
|
+
|
|
|
+ str1 += `</${someTree.subTags[i].tagName}>`;
|
|
|
+}
|
|
|
+
|
|
|
+str1 += `</${someTree.tagName}>`;
|
|
|
+
|
|
|
+document.write(str1);
|
|
|
+
|
|
|
+
|
|
|
+//destruct array
|
|
|
+let arr = [1,2,3,4,5, "a", "b", "c"];
|
|
|
+
|
|
|
+let [odd1, even1, odd2, even2, odd3, ...stringArr] = arr;
|
|
|
+
|
|
|
+
|
|
|
+//destruct string
|
|
|
+let arr1 = [1, "abc"];
|
|
|
+
|
|
|
+let [number, [s1, s2, s3]] = arr1;
|
|
|
+
|
|
|
+
|
|
|
+//destruct 2
|
|
|
+let obj = {name: 'Ivan',
|
|
|
+ surname: 'Petrov',
|
|
|
+ children: [{name: 'Maria'}, {name: 'Nikolay'}]};
|
|
|
+
|
|
|
+let [name1, name2] = obj.children;
|
|
|
+
|
|
|
+
|
|
|
+//Задание на черный пояс
|
|
|
+let predictArray = {
|
|
|
+ 11: 1,
|
|
|
+};
|
|
|
+
|
|
|
+let history = "11";
|
|
|
+
|
|
|
+let userChose = 1;
|
|
|
+
|
|
|
+while(userChose) {
|
|
|
+ userChose = prompt("Введите 1 или 2");
|
|
|
+
|
|
|
+ alert(`Предсказываю ${predictArray[history]}`);
|
|
|
+
|
|
|
+ predictArray[history] = +userChose;
|
|
|
+
|
|
|
+ history += userChose;
|
|
|
+ history = history.slice(1);
|
|
|
+}
|