//3 persons
let a = {
    name: 'Layne',
    surname: 'Staley',
}

let b = {
    name: 'Varg',
    surname: 'Vikernes'
}

let c = {
    name: 'Alexi',
    surname: 'Laiho'
}

//different fields
a.sex = 'male'
a.fathername = 'Phil'
b.project = 'Burzum'
c.age = 41
c.project = 'Children of Bodom'

//fields check
    // let arr = [a, b, c]
    // for (let i = 0; i < arr.length; i++) {
    //     if ('sex' in arr[i]) {
    //         alert(arr[i].sex)
    //     }
    //     if ('project' in arr[i]) {
    //         alert(arr[i].project)
    //     }
    //     if ('age' in arr[i]) {
    //         alert(arr[i].age)
    //     }
    //     if ('fathername' in arr[i]) {
    //         alert(arr[i].fathername)
    //     }
    // }

//array of persons
let persons = [a, b, c, {name: 'James', surname: 'Hetfield', age: 58, fathername: 'Virgil'}]

//loop of persons
    // for (let i = 0; i < persons.length; i++) {
    //     console.log(persons[i])
    // }

//loop of name and surname
    // for (let i = 0; i < persons.length; i++) {
    //     console.log(persons[i].name, persons[i].surname)
    // }

//loop of loop of values
    // for (let i = 0; i < persons.length; i++) {
    //     for (let key in persons[i]) {
    //         console.log(persons[i][key])
    //     }
    // }

    // or
    
    // for (let i = 0; i < persons.length; i++) {
    //     for (let [key, value] of Object.entries(persons[i])) {
    //         console.log(value)
    //     }
    // }

//fullName
for (let i = 0; i < persons.length; i++) {
    if ('fathername' in persons[i]) {
        persons[i].fullName = persons[i].name + ' ' + persons[i].surname
    }
}

//serialize
JSON.stringify(persons)

//deserialize
persons[persons.length] = JSON.parse('{"name": "Peeter", "surname": "Steele", "project": "Type O Negative"}')

//HTML
    // let str = "<table border='1'>"
    // for (let i = 0; i < persons.length; i++) {
    //     if (i === 0) {
    //         str += `<tr><th></th><th>Name</th><th>Surname</th></tr>`
    //     } else {
    //         str += `<tr><td>${i}</td><td>${persons[i].name}</td><td>${persons[i].surname}</td></tr>`
    //     }  
    // }
    // str += "</table>"

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

//HTML optional fields
    // let columns = {}
    // for (let i = 0; i < persons.length; i++) {
    //     Object.assign(columns, persons[i])
    // }
    // let str = '<table border="1">'
    // for (let i = 0; i <= persons.length; i++) {
    //     str += '<tr>'
    //     if (i === 0) {
    //         str += '<td></td>'
    //         for (let [key, value] of Object.entries(columns)) {
    //             str += `<td> ${key} </td>`
    //         }
    //     } else {
    //         str += `<td>${i}</td>`
    //         for (let [key, value] of Object.entries(columns)) {
    //             if (key in persons[i-1]) {
    //                 str += `<td> ${persons[i-1][key]} </td>`
    //             } else {
    //                 str += `<td> </td>`
    //             }
                
    //         }
    //     }
    //     str += '</tr>'
    // }
    // str += '</table>'
    // document.write(str)

//HTML tr color
    // let columns = {}
    // for (let i = 0; i < persons.length; i++) {
    //     Object.assign(columns, persons[i])
    // }
    // let str = '<table border="1">'
    // for (let i = 0; i <= persons.length; i++) {
    //     if (i % 2) {
    //         str += '<tr>'
    //     } else {
    //         str += '<tr style="background-color: rgba(255, 0, 0, 0.2)">'
    //     }
        
    //     if (i === 0) {
    //         str += '<td></td>'
    //         for (let [key, value] of Object.entries(columns)) {
    //             str += `<td> ${key} </td>`
    //         }
    //     } else {
    //         str += `<td>${i}</td>`
    //         for (let [key, value] of Object.entries(columns)) {
    //             if (key in persons[i-1]) {
    //                 str += `<td> ${persons[i-1][key]} </td>`
    //             } else {
    //                 str += `<td> </td>`
    //             }
                
    //         }
    //     }
    //     str += '</tr>'
    // }
    // str += '</table>'
    // document.write(str)

//HTML th optional
//Я уже случайно это сделала кажется, тут просто заменила свои td на th в нужном месте
let columns = {}
for (let i = 0; i < persons.length; i++) {
    Object.assign(columns, persons[i])
}
let str = '<table border="1">'
for (let i = 0; i <= persons.length; i++) {
    if (i % 2) {
        str += '<tr>'
    } else {
        str += '<tr style="background-color: rgba(255, 0, 0, 0.2)">'
    }
    
    if (i === 0) {
        str += '<th></th>'
        for (let key of Object.keys(columns)) {
            str += `<th> ${key} </th>`
        }
    } else {
        str += `<td>${i}</td>`
        for (let key of Object.keys(columns)) {
            if (key in persons[i-1]) {
                str += `<td> ${persons[i-1][key]} </td>`
            } else {
                str += `<td> </td>`
            }
            
        }
    }
    str += '</tr>'
}
str += '</table>'
document.write(str)

//Задание на синий пояс
var someTree = {
    tagName: "table", 
    subTags: [ 
        {
                    tagName: "tr",
                    subTags: [
                        {
                            tagName: "td",
                            text: "some text",
                        },
                        {
                            tagName: "td",
                            text: "some text 2",
                        }
                    ]
        }
    ],
    attrs: 
    {
        border: 1,
    },
}

function tableConstructor (object) {

    let table = `<${object.tagName}`
    for (let [key, value] of Object.entries(object.attrs)) {
        table +=  ` ${key}="${value}"`
    }
    table += `>`
    
    for (let i = 0; i < object.subTags.length; i++) {
        table += `<${object.subTags[i].tagName}>`
        if ('subTags' in object.subTags[i]) {
            for (let j = 0; j < object.subTags[i].subTags.length; j++) {
                table += `<${object.subTags[i].subTags[j].tagName}>`
                if ('text' in object.subTags[i].subTags[j]) {
                    table += `${object.subTags[i].subTags[j].text}`
                }
                table += `</${object.subTags[i].subTags[j].tagName}>`
            }
        }
        table += `</${object.subTags[i].tagName}>`
    }
    table += `</${object.tagName}>`

    document.write(table)
}

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

    // let [even1, even2] = arr.filter(element => !(element % 2))
    // let [odd1, odd2, odd3] = arr.filter(element => element % 2)
    // let oneMoreArr = arr.filter(element => !(+element))

//destruct string
    // let arr = [1, "abc"]

    // let [number] = arr.filter(element => +element)
    // let [[s1, s2, s3]] = arr.filter(element => !(+element))

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

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

//destruct 3
    // let arr = [1,2,3,4,5,6,7,10]
    // let {0:a, 1:b, length} = arr;

//Задание на черный пояс
let predictArray = {
    "0000": -1,
    "0001": -1,
    "0010": -1,
    "0100": -1,
    "1000": -1,
    "0011": -1,
    "0110": -1,
    "1100": -1,
    "1001": -1,
    "0101": -1,
    "1010": -1,
    "1110": -1,
    "0111": -1,
    "1011": -1,
    "1101": -1,
    "1111": -1,
}

let history = "1111"
let points = 0;
let counter = 0;
while (true) {
    if (predictArray[history] === -1) {
        console.log('Предсказание гадалки: ' + (predictArray[history] = Math.round(Math.random())))
    } else {
        console.log('Предсказание гадалки: ' + predictArray[history])
    }

    let userNumber = prompt('Введите 0 или 1')
    if (userNumber === null || userNumber === '') {
        break
    }
    console.log('Ввод пользователя: ' + userNumber)
    counter++

    if (predictArray[history] == userNumber) {
        points--
    } else {
        points++
    }
    alert('Ваш счёт: ' + points)

    predictArray[history] = +userNumber;
    history = history.split('');
    history.shift();
    history.push(+userNumber);
    history = history.join('')
}

alert('Количество ходов: ' + counter + '. Ваш счёт: ' + points)


