//3 persons
let a = {
    name: "Petro",
    surname: "Petrov",
}
let b = {
    name: "Ivan",
    surname: "Ivanov",
}
let c = {
    name: "Nikita",
    surname: "Nikitin",
}

//different fields
a.age = 21;
b.sex = "male";
c.fathername = "Ivanovi4"

//fields check
let optionalField = ["age", "sex", "fathername"];
let userChoise = prompt("Выберите массив (a, b, c)");

if (userChoise === "a") {
    for (let i of optionalField) {
        if (i in a) {
            alert("Необязательное поле в массиве a: " + i);
        }
    }
} else if (userChoise === "b") {
    for (let i of optionalField) {
        if (i in b) {
            alert("Необязательное поле в массиве b: " + i);
        }
    }
} else if (userChoise === "c") {
    for (let i of optionalField) {
        if (i in c) {
            alert("Необязательное поле в массиве c: " + i);
        }
    }
} else {
    alert("Неправильный ввод")
}

//array of persons
let persons = [a, b, c];

let d = { name: "Katya", surname: "Popova" };

persons[persons.length] = d;

//loop of persons
for (let i in persons) {
    console.log(persons[i]);
}

//loop of name and surname
for (let i in persons) {
    console.log(persons[i]["name"], persons[i]["surname"])
}

//loop of loop of values
for (let i in persons) {
    for (let j in persons[i]) {
        console.log(persons[i][j]);
    }
}

//fullName
for (let i in persons) {
    if (persons[i]["fathername"]) {
        persons[i].fullName = persons[i]["name"] + persons[i]["fathername"] + persons[i]["surname"];
    } else {
        persons[i].fullName = persons[i]["name"] + persons[i]["surname"];
    }
}

//serialize
JSON.stringify(persons);

//deserialize
JSON.parse("[{\"name\":\"Petro\",\"surname\":\"Petrov\",\"age\":21,\"fullName\":\"PetroPetrov\"},{\"name\":\"Ivan\",\"surname\":\"Ivanov\",\"sex\":\"male\",\"fullName\":\"IvanIvanov\"},{\"name\":\"Nikita\",\"surname\":\"Nikitin\",\"fathername\":\"Ivanovi4\",\"fullName\":\"NikitaIvanovi4Nikitin\"},{\"name\":\"Katya\",\"surname\":\"Popova\",\"fullName\":\"KatyaPopova\"}]")

//HTML

let str = "<table border='1'>";
str += `<tr><td>name</td><td>surname</td></tr>`;

for (let i in persons) {
    str += `<tr><td>${persons[i]["name"]}</td><td>${persons[i]["surname"]}</td></tr>`;
}
str += "</table>";

console.log(str);
document.write(str);

//HTML optional fields
let str2 = "<table border='1'>";
str2 += `<tr><td>Name</td><td>Surname</td><td>Age</td><td>Sex</td><td>Fathername</td><td>FullName</td></tr>`;

for (let i in persons) {
    str2 += `<tr><td>${persons[i]["name"]}</td><td>${persons[i]["surname"]}</td><td>${persons[i]["age"] || ""}</td><td>${persons[i]["sex"] || ""}</td><td>${persons[i]["fathername"] || ""}</td><td>${persons[i]["fullName"]}</td></tr>`;
}

str2 += "</table>";

console.log(str2);
document.write(str2);


//HTML tr color
let str3 = "<table border='1'>";
str3 += `<tr><td>Name</td><td>Surname</td><td>Age</td><td>Sex</td><td>Fathername</td><td>FullName</td></tr>`;

for (let i in persons) {
    if (i % 2 === 0) {
        str3 += "<tr style='background: blue'>";
    } else {
        str3 += "<tr style='background: orange'>";
    }
    str3 += `<td>${persons[i]["name"]}</td><td>${persons[i]["surname"]}</td><td>${persons[i]["age"] || ""}</td><td>${persons[i]["sex"] || ""}</td><td>${persons[i]["fathername"] || ""}</td><td>${persons[i]["fullName"]}</td></tr>`;
}

str3 += "</table>";

console.log(str3);
document.write(str3);

//HTML th optional
let str4 = "<table border='1'>";

let arrayOfKeys = [];

for (let i in persons) {                                            //запись ключей со всех персон
    arrayOfKeys[i] = Object.keys(persons[i]);
}

arrayOfKeys = [...(new Set(arrayOfKeys.toString().split(",")))];    //делаем простой массив и удаляем повтор.ключи

str4 += "<tr>"
for (let key in arrayOfKeys) {
    str4 += `<td>${arrayOfKeys[key]}</td>`
}
str4 += "</tr>"
console.log(arrayOfKeys)

for (let i in persons) {
    str4 += "<tr>"
    for (let key of arrayOfKeys) {
        if (persons[i][key] === undefined) {
            str4 += "<td> </td>"
        } else {
            str4 += `<td>${persons[i][key]}</td>`
        }
    }
    str4 += "</tr>"
}

str4 += "</table>";

console.log(str4);
document.write(str4);

//Задание на синий пояс
var someTree = {
    tagName: "table", //html tag
    subTags: [ //вложенные тэги
        {
            tagName: "tr",
            subTags: [
                {
                    tagName: "td",
                    text: "some text",
                },
                {
                    tagName: "td",
                    text: "some text 2",
                }
            ],
            attrs:
            {
                class: "b"
            },
        },
        {
            tagName: "tr",
            subTags: [
                {
                    tagName: "td",
                    text: "some text",
                }
            ],
            attrs:
            {
                class: "a"
            },
        }
    ],
    attrs:
    {
        border: 1,
        class: "bb"
    },
}


let tagAttrs = "";
let strSubTags = "";
let subTagsAttrs = "";
let subTagsAttrsInner = "";

for (let key in someTree.attrs) {
    tagAttrs += key + "=" + someTree.attrs[key] + " ";
}

for (let i of someTree.subTags) {

    for (let j in i.attrs) {
        subTagsAttrs = j + "=" + i.attrs[j] + " ";
    }
    strSubTags += `<${i.tagName + " " + subTagsAttrs}>`

    if ("subTags" in i) {
        for (let k of i.subTags) {
            for (let l in k.attrs) {
                subTagsAttrsInner = l + "=" + k.attrs[l] + " ";
            }
            strSubTags += `<${k.tagName + " " + subTagsAttrsInner}>${k.text}</${k.tagName}>`
        }
    }
    strSubTags += `</${i.tagName}>`
}

let str = `<${someTree.tagName + " " + tagAttrs}>${strSubTags}</${someTree.tagName}>`;
console.log(str)
document.write(str);

//destruct array
let arr = [1, 2, 3, 4, 5, "a", "b", "c"];

let [odd1, even1, odd2, even2, odd3, ...array] = arr;

//destruct string
let arr2 = [1, "abc"];

let [number, [s1, s2, s3]] = arr2;

//destruct 2
let obj = {
    name: 'Ivan',
    surname: 'Petrov',
    children: [{ name: 'Maria' }, { name: 'Nikolay' }]
}

let { children: [{ name: name1 }, { name: name2 }] } = obj;

//destruct 3
let arr3 = [1, 2, 3, 4, 5, 6, 7, 10];

let { length, 0: a, 1: b } = arr3;