
// htmlTree()
function htmlTree() {
   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'
                  }
              ]
          }
      ]
   }

   console.log(body.children[1].children[1].children)
   console.log(body.children[0].children[3].attrs.id)  
}

// declarativeFields()
function declarativeFields() {
    var notebook = {
        brand: prompt("Enter a brand",""),
        type:  prompt("Enter a type",""),
        model: prompt("Enter a model",""),
        ram: +prompt("Enter amount of ram (gb)",""),
        size: prompt("Enter a size of screen (inch)",""),
        weight: +prompt("Enter a weight (kg)",""),
        resolution: {
            width: +prompt("Enter a resolution width (px)",""),
            height: +prompt("Enter a resolution height (px)",""),
        },
    };
    console.log(notebook);   
    
    var phone = {
        brand: prompt("Enter a brand",""),
        model: prompt("Enter a model",""),
        ram: +prompt("Enter amount of ram (gb)",""),
        color: prompt("Enter a color",""),
    };
    console.log(phone);
    
    var person = {
      name: prompt("Enter a name",""),
      surname: prompt("Enter a surname",""),
      married: confirm("Is person married?",""),
    }
    console.log(person);
}

// tableMult()
function tableMult() {
    let str = '<table>'
    for (let rowIndex=1; rowIndex<10; rowIndex++) {
        str += '<tr>'
        for (let j=1; j<10; j++) {
            str += `<td>${rowIndex*j}</td>`
        }
        str += '</tr>'
    }
    str += '</table>'
    document.write(str)
}

// contArr()
function contArr() {
    let inputArr = [1,2,3,4,5,6,'qqq',true]
    let newElement = 0
    for (let i=0; i<inputArr.length; i++) {
        newElement += inputArr[i]
    }
    inputArr.push(newElement)
    console.log(inputArr)
}

// tableMultObj()
function tableMultObj() {
    let phone = {
        brand: "meizu",
        model: "m2",
        ram: 2,
        color: "black",
        uuuuu: true
    }
    let str = '<table>\n'
    for (const [key, value] of Object.entries(phone)) {
        str += `    \n<tr><td><b>${key}</b></td><td>${value}</td></tr>\n`
    }
    str += '</table>'
    document.write(str)
}

// objectLinks()
function objectLinks() {
   var notebook = {
      brand: "HP",
      type:  "440 G4",
      model: "Y7Z75EA",
      ram: 4,
      size: "14",
      weight: 1.8,
      resolution: {
          width: 1920,
          height: 1080,
      },
   };
   
   var phone = {
      brand: "meizu",
      model: "m2",
      ram: 2,
      color: "black",
   };
   
   var person = {
      name: "Donald",
      surname: "Trump",
      married: true,    
   };
   person.smartphone = phone;
   person.laptop = notebook;
   phone.owner = person;
   notebook.owner = person;

   console.log(person.smartphone.owner.laptop.owner.smartphone == person.smartphone);
}


// imperativeArrayFill3()
function imperativeArrayFill3() {
   let arr = [];
   arr[0] = prompt("Вводи элемент массива","")
   arr[1] = prompt("Вводи элемент массива","")
   arr[2] = prompt("Вводи элемент массива","")
   console.log(arr);
}

// whileConfirm()
function whileConfirm() {
   let a = true;
   while (a) {
      a = confirm('Продолжить цикл?')
   }  
}

// arrayFill()
function arrayFill() {
   let arr = [];
   let res = null;
   do {
      res = prompt("Вводи элемент массива","");
      if (res !== null) {
         arr.push(res);
      } 
   } while (res !== null)
   console.log(arr);
}

// arrayFillNopush()
function arrayFillNopush() {
   let arr = [];
   let res = null;
   let i = 0;
   do {
      res = prompt("Вводи элемент массива","");
      if (res !== null) {
         arr[i] = res;
      } 
      i++;
   } while (res !== null)
   console.log(arr);
}

// infiniteProbability()
function infiniteProbability() {
   let i = 0;
   while (true) {
      if (Math.random() > 0.9) {
         break;
      }
      i++; 
   }
   console.log(`Количество итераций: ${i}`);
}

// emptyLoop()
function emptyLoop() {
   for (let i = 0; i !== null; i = prompt("Продолжить цикл?","")) {}
}

// progressionSum()
function progressionSum() {
   let n = Number(prompt("До какого числа продолжать прогрессию?",""));
   let sum = 0;
   for (let i = 1; i <= n; i += 3) {      
      sum += i;
   }
   console.log(sum);
}

// chessOneLine()
function chessOneLine() {
   let size = +prompt("Введите длинну строки","");
   let str = '';
   for (let i = 0; i < size; i++) {
      if (i % 2 === 0) {
         str += ' ';
      } else {
         str += '#';
      }
   }
   console.log(str);
}

// numbers()
function numbers() {
   let str = ''
   for (let i=0; i < 10; i++) {
       for (let j=0; j < 10; j++) {
           str += `${j}`
       }
       str += '\n'
   }
   console.log(str)
}

// chess()
function chess() {
   let size = +prompt("Введите размер поля","")

   let str = ''
   let counter = 0
   while (counter < size) {
      counter++
      if (counter % 2 === 0) {
         for (let counter2 = 0; counter2 < size; counter2++) {
               if (counter2 % 2 === 0) {
                  str += '.'
               } else {
                  str += '#'
               }
         }
         str += '\n'
      } else {
         for (let counter3 = 0; counter3 < size; counter3++) {
               if (counter3 % 2 === 0) {
                  str += '#'
               } else {
                  str += '.'
               }
               }
               str += '\n'
      }
   }
   console.log(str)
}

// cubes()
function cubes() {
   let arr = []
   let inputNumber = +prompt("Введите длинну массива","")
   let i = 0
   while (i < inputNumber) {
      let element = (i ** 3)
      arr.push(element)
      i++
   }
   console.log(arr)
}

// multiplyTable()
function multiplyTable() {
   let arr = []
   for (let i=0; i < 10; i++) {
      arr[i] = []
      for (let j=0; j < 10; j++) {
         arr[i].push(i*j) 
      }       
   }
   console.log(arr)
   return arr
}

// matrixToHtmlTable()
function matrixToHtmlTable() {
   let inputArr = multiplyTable()
   let str = '<table>'
   for (let i = 0; i < inputArr.length; i++) {
       str += '<tr>'
       for (let j = 0; j < inputArr[i].length; j++) {
           str += `<td>${inputArr[i][j]}</td>`
       }
       str += '</tr>'
   }
   str += '</table>'
   document.write(str)
}

// triangle()
function triangle() {
   let height = +prompt("Введите высоту треугольника","")
   let width = (height * 2 - 1)
   let arr = []
   let str = ''

   for (let colCounter = 0; colCounter < width; colCounter++) {
      arr[colCounter] = []
      for (rowCounter = 0; rowCounter < height; rowCounter++) {
         if (colCounter < height) {
            if (rowCounter <= colCounter) {
               arr[colCounter].push('#')
            } else {
               arr[colCounter].push('.')
            }   
         } else {
            if (rowCounter < (width - colCounter)) {
               arr[colCounter].push('#')
            } else {
               arr[colCounter].push('.')
            }
         }
      }
   }
//   console.log(arr)

  for (let j = 1; j <= arr[0].length; j++) {
      for (let i = 0; i < arr.length; i++) {
         str += `${arr[i][arr[i].length - j]}`
      }
      str += '\n'
   }
   console.log(str)
}

// fortuneTeller()
function fortuneTeller() {
   let history = [1,1,1,1]
   let predictArray = [[[[],[]],[[],[]]],[[[],[]],[[],[]]]]

   let loop = true;
   while (loop) {

      let compOutput = null
      if (predictArray[history[0]][history[1]][history[2]][history[3]] || predictArray[history[0]][history[1]][history[2]][history[3]] === 0) {
         compOutput = predictArray[history[0]][history[1]][history[2]][history[3]]
         console.log(compOutput)
      } else {
         compOutput = Math.floor(Math.random()*2)
         console.log(compOutput)
      }

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

      predictArray[history[0]][history[1]][history[2]][history[3]] = userInput
      // console.log(predictArray)

      history.push(userInput)
      history.shift()
      // console.log(history)

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










