Browse Source

HW js05 part

Ivar 2 years ago
parent
commit
6dcdcf4c36
2 changed files with 343 additions and 11 deletions
  1. 1 1
      js/04/index.js
  2. 342 10
      js/05/index.js

+ 1 - 1
js/04/index.js

@@ -369,7 +369,7 @@ function triangle() {
    console.log(str)
 }
 
-fortuneTeller ()
+fortuneTeller()
 function fortuneTeller() {
    history = [1,1,1,1]
 

+ 342 - 10
js/05/index.js

@@ -1,24 +1,356 @@
-htmlTree()
-function htmlTree() {
 
+// persons3()
+function persons3() {
+   let a = {
+      name: 'Vasya',
+      surname: 'Pupkin',
+   }
+   let b = {
+      name: 'Petya',
+      surname: 'Petrov',
+   }
+   let c = {
+      name: 'Ivan',
+      surname: 'Ivanov',
+   }
 }
 
-htmlTree()
-function htmlTree() {
+// 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 (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)
 }
 
-htmlTree()
-function htmlTree() {
+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'
+                  }
+              ]
+          }
+      ]
+   }
+}
+
+// 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)
 }
 
-htmlTree()
-function htmlTree() {
+// 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)
 }
 
-htmlTree()
-function htmlTree() {
+fortuneTellerObj()
+function fortuneTellerObj() {
 
 }