
// persons3()
function persons3() {
   let a = {
      name: 'Vasya',
      surname: 'Pupkin',
   }
   let b = {
      name: 'Petya',
      surname: 'Petrov',
   }
   let c = {
      name: 'Ivan',
      surname: 'Ivanov',
   }
}

// differentFields()
function differentFields() {
   let a = {
      name: 'Vasya',
      surname: 'Pupkin',
      age: 25,
      fathername: 'Pupkovich',
   }
   let b = {
      name: 'Petya',
      surname: 'Petrov',
      fathername: 'Petrovich',
      sex: true,
   }
   let c = {
      name: 'Ivan',
      surname: 'Ivanov',
      age: 80,
      sex: false,
   }
}

// fieldsCheck()
function fieldsCheck() {
   let a = {
      name: 'Vasya',
      surname: 'Pupkin',
      age: 25,
      fathername: 'Pupkovich',
   }
   let b = {
      name: 'Petya',
      surname: 'Petrov',
      fathername: 'Petrovich',
      sex: true,
   }
   let c = {
      name: 'Ivan',
      surname: 'Ivanov',
      age: 80,
      sex: false,
   }
   let obj = b
   if ('age' in obj) {
      alert(`age: ${obj.age}`)
   }
   if ('fathername' in obj) {
      alert(`fathername: ${obj.fathername}`)
   }
   if ('sex' in obj) {
      alert(`sex: ${obj.sex}`)
   }
}

// arrayOfPersons()
function arrayOfPersons() {
   let a = {
      name: 'Vasya',
      surname: 'Pupkin',
      age: 25,
      fathername: 'Pupkovich',
   }
   let b = {
      name: 'Petya',
      surname: 'Petrov',
      age: 30,
      fathername: 'Petrovich',
      sex: true,
   }
   let c = {
      name: 'Ivan',
      surname: 'Ivanov',
      age: 80,
      sex: false,
   }
   let persons = [{
      name: 'Literal',
      surname: 'Literalov',
      fathername: 'Literalovich',
      sex: true,
   },]
   persons.push(a, b, c, deserialize())
   return persons   
}

// loopOfPersons()
function loopOfPersons() {
   let persons = arrayOfPersons()
   for (const element of persons) {
      console.log(element)
   }
}

// loopOfNameAndSurname()
function loopOfNameAndSurname() {
   let persons = arrayOfPersons()
   for (const element of persons) {
      console.log(`${element.name} ${element.surname}`)
   }
}

// loopOfLoopOfValues()
function loopOfLoopOfValues() {
   let persons = arrayOfPersons()
   for (const element of persons) {
      for (let [key, value] of Object.entries(element)) {
         console.log(`${key}: ${value}`)
      }
   }
}

// fullName()
function fullName() {
   let persons = arrayOfPersons()
   for (const element of persons) {
      element.fullName = element.name + ' ' + element.surname + ' ' + (element.fathername || '')
   }
   console.log(persons)
}

// serialize()
function serialize() {
   let persons = arrayOfPersons()
   let json = JSON.stringify(persons)
   console.log(json)   
}

// deserialize()
function deserialize() {
   let json = JSON.stringify({
      "name": "Jason",
      "surname": "Stetham",
      "age": "40"
   })
   let jason = JSON.parse(json)
   // console.log(json)
   // console.log(jason)   
   return jason
}

// HTML()
function HTML() {
   let persons = arrayOfPersons()
   let str = "<table border='1'>"
   for (const element of persons) {
      str += `<tr><td>${element.name}</td><td>${element.surname}</td></tr>`
   }
   str += "</table>"

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

// HTMLOptionalFields()
function HTMLOptionalFields() {
   let persons = arrayOfPersons()
   let sampleLength  = 0
   let sampleKeys = null
   let str = "<table border='1'>"
   for (const element of persons) {
      if (sampleLength < Object.keys(element).length) {         
         sampleKeys = Object.keys(element)
         sampleLength = sampleKeys.length
      }
   }

   for (const element of persons) {
      str += '<tr>'      
      for (let i = 0; i < sampleLength; i++) {
         if (sampleKeys[i] in element) {
            str += `<td>${element[sampleKeys[i]]}</td>`
         } else {
            str += `<td></td>`
         }
      }
      str += '</tr>'
   }
   str += "</table>"

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

// HTMLTrColor()
function HTMLTrColor() {
   let persons = arrayOfPersons()
   let sampleLength  = 0
   let sampleKeys = null
   let str = "<table border='1'>"
   for (const element of persons) {
      if (sampleLength < Object.keys(element).length) {         
         sampleKeys = Object.keys(element)
         sampleLength = sampleKeys.length
      }
   }
   let j = 0
   for (const element of persons) {
      if (j % 2 === 0) {
         str += '<tr style="background-color: #999999">'
      } else {
         str += '<tr>'      
      }
      for (let i = 0; i < sampleLength; i++) {         
         if (sampleKeys[i] in element) {
            str += `<td>${element[sampleKeys[i]]}</td>`
         } else {
            str += `<td></td>`
         }
      }
      str += '</tr>'
      j++
   }
   str += "</table>"

   document.write(str)
}

// HTMLThOptional()
function HTMLThOptional() {
   let persons = arrayOfPersons()
   let sampleLength  = 0
   let sampleKeys = null
   let str = "<table border='1'>"
   for (const element of persons) {
      if (sampleLength < Object.keys(element).length) {         
         sampleKeys = Object.keys(element)
         sampleLength = sampleKeys.length
      }
   }
   let j = 0
   for (let n = 0; n < persons.length; n++) {
      let element = persons[n]

      if (n === 0) {
         str += '<tr>'      
         for (let i = 0; i < sampleLength; i++) {  
            str += `<th>${sampleKeys[i]}</th>`
         }
         str += '</tr>'
      } 

      if (j % 2 === 0) {
         str += '<tr style="background-color: #999999">'
      } else {
         str += '<tr>'      
      }

      for (let i = 0; i < sampleLength; i++) {  
                
         if (sampleKeys[i] in element) {
            str += `<td>${element[sampleKeys[i]]}</td>`
         } else {
            str += `<td></td>`
         }
      }
      str += '</tr>'
      j++
   }
   str += "</table>"

   document.write(str)
}

HTMLConstructor()
function HTMLConstructor() {
   let body = {
      tagName: 'body',
      attrs: {},
      children: [
          {   tagName: 'div',
              attrs: {},
              children: [
                  {   tagName: 'span',
                      attrs: {},
                      children: 'Enter a data please'    
                  },
                  {   tagName: 'br',
                      attrs: {}
                  },
                  {   tagName: 'input',
                      attrs: {
                          type: 'text',
                          id: 'name'
                      }
                  },
                  {   tagName: 'input',
                      attrs: {
                          type: 'text',
                          id: 'surname'
                      }
                  }
              ]
          },
          {   tagName: 'div',
              attrs: {},
              children: [
                  {   tagName: 'button',
                      attrs: {
                          id: 'ok'
                      },
                      children: 'OK'
                  },
                  {   tagName: 'button',
                      attrs: {
                          id: 'cansel'
                      },
                      children: 'Cancel'
                  }
              ]
          }
      ]
   }

// если у тега нет детей, то считется, что он одиночный

   let str = ''
   str += `<${body.tagName}`
      if (body.attrs) {
         for (let [key, value] of Object.entries(body.attrs)) {
            str += ` ${key}="${value}"`
         }
      }
      if (body.children) {
         str += `>`   
         if (typeof body.children === 'object') {
            for (let obj of body.children) {

               str += `<${obj.tagName}`
               if (obj.attrs) {
                  for (let [key, value] of Object.entries(obj.attrs)) {
                     str += ` ${key}="${value}"`
                  }
               }
               if (obj.children) {
                  str += `>`

                  if (typeof obj.children === 'object') {

                     for (let obj2 of obj.children) {
            
                        str += `<${obj2.tagName}`
                        if (obj2.attrs) {
                           for (let [key2, value2] of Object.entries(obj2.attrs)) {
                              str += ` ${key2}="${value2}"`
                           }
                        }
      
                        if (obj2.children) {
                           str += `>`

                           if (typeof obj2.children === 'object') {

                           } else {
                              str += `${obj2.children}`
                           }
                           str += `</${obj2.tagName}>`  
                        } else {
                           str += `/>`
                        }
      
                     }
                  } else {
                     str += `${obj.children}`
                  }
                        
                  str += `</${obj.tagName}>` 
               } else {
                  str += `/>`
               }
 
            }
         } else {
            str += `${body.children}`
         }
         str += `</${body.tagName}>`
      } else {
         str += `/>`
      }

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

// destructArray()
function destructArray() {
   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)
}

// destructString()
function destructString() {
   let arr = [1, "abc"]
   let [number, [s1, s2, s3]] = arr
   console.log(number, s1, s2, s3)
}

// destruct2()
function destruct2() {
   let obj = {
      name: 'Ivan',
      surname: 'Petrov',
      children: [{name: 'Maria'}, {name: 'Nikolay'}]
   }
   let {children: [{name: name1}, {name: name2}]} = obj
   console.log(name1, name2)
}

// destruct3()
function destruct3() {
   let arr = [1,2,3,4,5,6,7,10]
   let {0:a, 1:b, length:length} = arr
   console.log(a, b, length)
}

// fortuneTellerObj()
function fortuneTellerObj() {
   let history = [1,1,1,1]
   let historyStr = '1111'
   let predictArray = {}

   let loop = true;
   while (loop) {

      let compOutput = null
      if (historyStr in predictArray) {
         compOutput = predictArray[historyStr]
         console.log(compOutput)
      } else {
         compOutput = Math.floor(Math.random()*2)
         console.log(compOutput)
      }

      let userInput = +confirm('Нажмите ОК, если выбираете 1\nНажмите Отмена, если выбираете 0')
      // console.log(userInput)

      predictArray[historyStr] = userInput
      // console.log(predictArray)

      history.push(userInput)
      history.shift()
      historyStr = history.join('')
      // console.log(historyStr)

      loop = confirm('Еще?')
   } 
}