Browse Source

hw5 all tasks till 'HTML optional fields' done

miskson 2 years ago
parent
commit
5897231cdf
2 changed files with 115 additions and 0 deletions
  1. 13 0
      hw5/index.html
  2. 102 0
      hw5/script.js

+ 13 - 0
hw5/index.html

@@ -0,0 +1,13 @@
+<!DOCTYPE html>
+<html lang="en">
+<head>
+    <meta charset="UTF-8">
+    <meta http-equiv="X-UA-Compatible" content="IE=edge">
+    <meta name="viewport" content="width=device-width, initial-scale=1.0">
+    <title>Homework 5</title>
+</head>
+<body>
+    <h1>Homework 5</h1>
+    <script src="./script.js"></script>
+</body>
+</html>

+ 102 - 0
hw5/script.js

@@ -0,0 +1,102 @@
+//3 persons
+let a = {
+    'Name': 'Sergei',
+    'surname': 'Levshnia',
+    'fathername': 'Sergeevich'
+}
+console.log(a['Name'], a.surname)
+
+let b = {
+    'Name': 'Thomas',
+    surname: 'Anderson'
+}
+console.log(b.Name, b.surname)
+
+let c = {
+    Name: 'Sylvester',
+    surname: 'Stallone'
+}
+console.log(c['Name'], c['surname'])
+
+//different fields
+a.age = 20
+b.nickname = 'Neo'
+c.movies = ['Rocky', 'Rambo: First Blood', 'Cobra', 'Over the Top']
+console.log(a, b, c)
+
+//fields Check
+function fieldCheck(obj, string) {
+    if(string in obj) {
+        alert(obj[string])
+    }
+}
+console.log(fieldCheck(a, 'age'))
+console.log(fieldCheck(b, 'awoken'))
+console.log(fieldCheck(c, 'movies'))
+
+//array of persons
+let persons = []
+persons.push(a, b, c)
+persons[persons.length] = {
+    Name: 'Billy',
+    surname: 'Joebob'
+}
+
+//loop of persons
+console.log('------------------------')
+console.log('loop of persons:')
+for(i in persons) {
+    console.log(persons[i])
+}
+
+//loop of name and surname
+console.log('------------------------')
+console.log('loop of name and surname:')
+for(i in persons) {
+    console.log(persons[i].Name, persons[i].surname)
+}
+
+//loop of loop of values
+console.log('------------------------')
+console.log('loop of loop of values:')
+for(let i in persons) {
+    for(let j in persons[i]) {
+        console.log(persons[i][j])
+    }
+    console.log('!-----------------------')
+}
+
+//fullName
+for(let i in persons) {
+    persons[i].fullName = `${persons[i].surname} ${persons[i].Name}` + (persons[i].fathername? ` ${persons[i].fathername}` : '')
+}
+
+//serialize
+let serialize = JSON.stringify(persons)
+console.log(serialize)
+
+//deserialize 
+persons.push(JSON.parse(serialize)[0])
+console.log(persons)
+
+//HTML
+let str = "<table border='1'>"
+for (let i = 0; i < persons.length; i++) {
+    str += `<tr><td>${persons[i].Name}</td><td>${persons[i].surname}</td></tr>`
+}
+str += "</table>"
+
+console.log(str)
+document.write(str)
+
+//Html optional fields
+let htmlOptional = '<h2>HTML optional fields</h2><table border="1">'
+for(let i = 0; i < persons.length; i++) {
+    htmlOptional += '<tr>'
+    for(let j in persons[i]) {
+        htmlOptional += `<td>${persons[i][j]}</td>`
+    }
+    htmlOptional += '</tr>'
+}
+htmlOptional += '</table>'
+document.write(htmlOptional)