
let persons = [
   {name: "Иван", age: 17},
   {name: "Мария", age: 35},
   {name: "Алексей", age: 73},
   {name: "Яков", age: 12},
]
// console.log(sort(persons, 'age', false))
function sort(array, field, increase = true) {
   let arr = []
   let personsUpdate = []
   for (let i = 0; i < array.length; i++) {
      arr.push(array[i][field])
   }
   if (field === 'name') {
      arr.sort()
   } else if (field === 'age') {
      arr.sort(compareNumeric)
      function compareNumeric(a, b) {
         if (a > b) return 1;
         if (a == b) return 0;
         if (a < b) return -1;
      }
   }
   // console.log(arr)  
   for (let i = 0; i < arr.length; i++) {
      for (let j = 0; j < array.length; j++) {
         if (arr[i] && arr[i] === array[j][field]) {
            personsUpdate.push(array[j])
         }
      }      
   }
   if (!personsUpdate[0]) {
      personsUpdate = array
   }

   if (increase) {
      return personsUpdate
   } else {
      return personsUpdate.reverse()
   }
}

let data = ["1", {}, null, undefined, "500", 700]
// console.log(arrayMap(data))
function arrayMap(array) {
   return array.map(item => {if (typeof(item) === 'string') {
      return Number(item)
   } else {
      return item
   }})   
}

let data2 = ["0", 5, 3, "string", null, 100n]
// console.log(arrayReduce(data2))
function arrayReduce(array) {
   let newArr = array.filter(item => typeof(item) === 'number')
   return newArr.reduce((sum, item) => sum + item, 0) 
}

let phone = {
   brand: "meizu",
   model: "m2",
   ram: 2,
   color: "black",
}
// objectFilter(phone, (key, value) => key == "color" || value == 2)
function objectFilter(object, func) {
   let result = {}
   for (const [key, value] of Object.entries(object)) {
      if (func(key, value)) {
         result[key] = value
      }
   }
   console.log(result)  
   return result
}

// objectMap({name: "Иван", age: 17}, function(key, value) {
//    let result = {};
//    result[key+"_"] = value + "$";
//    return result;
// })
function objectMap(object, func) {
   let result = {}
   for (const [key, value] of Object.entries(object)) {
      result = {...result, ...func(key, value)}
      // Object.assign(result, func(key, value))
   }
   console.log(result)  
   return result
}

// console.log(sumRec(4, 2, 3))  
function sumRec(length, a1, d) {
   if (length < 1) {
      return 0
   } else if (length === 1) {
      return a1
   } else {
      return a1 + sumRec((length - 1), a1 + d, d)
   }
}



htmlTreeRecWrapp()
function htmlTreeRecWrapp() {

   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 = ''

   htmlTreeRec(body, 0)
   function htmlTreeRec(object, level = 0) {

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

                  htmlTreeRec(child, level + 1)

               }
            } else {
               str += `${object.children}`
            }
            str += `</${object.tagName}>`
         } else {
            str += `/>`
         }    
   }
   console.log(str)   
   document.write(str)
}
