miskson hace 2 años
padre
commit
4750689ecc
Se han modificado 1 ficheros con 102 adiciones y 9 borrados
  1. 102 9
      hw5/script.js

+ 102 - 9
hw5/script.js

@@ -30,9 +30,9 @@ function fieldCheck(obj, string) {
         alert(obj[string])
     }
 }
-// console.log(fieldCheck(a, 'age'))
-// console.log(fieldCheck(b, 'awoken'))
-// console.log(fieldCheck(c, 'movies'))
+fieldCheck(a, 'age')
+fieldCheck(b, 'awoken')
+fieldCheck(c, 'movies')
 
 //array of persons
 let persons = []
@@ -117,7 +117,7 @@ document.write(htmlOptional)
 
 // blue belt
 class Markup {
-    constructor(tagName, [...nestedTags]=[],{...attrs}={},text='') {
+    constructor(tagName, [...nestedTags]=[], {...attrs}={}, text='') {
         this.tagName = tagName,
         this.nestedTags = nestedTags
         this.attrs = attrs
@@ -125,8 +125,101 @@ class Markup {
     }
 }
 
-//let md = new Markup('body', [[123],[123]], {a:228, b:1337}, 'sometext')
-let md = new Markup('body', [new Markup('h1',[],{},'header text')],{style:'background-color: red'}, '')
-let md2 = new Markup('body', [new Markup('h2')],{style:'background-color: red'}, '')
-let md3 = new Markup('table', [new Markup('tr', [new Markup('td',[],{},'sometext'),new Markup('td',[],{},'sometext') ])])
-console.dir(md3)
+
+let md3 = new Markup('table', [new Markup('tr', [new Markup('td', undefined, undefined, 'some text'),new Markup('td', undefined, undefined, 'some text2')])],{border: 1})
+console.dir(md3)
+
+let HTMLstr = `<h2>Blue belt</h2><${md3.tagName}`
+if(md3.attrs.length !== 0) {
+    for(let i in md3.attrs) {
+        HTMLstr += ` ${i}="${md3.attrs[i]}" `
+    }
+}
+HTMLstr += '>'
+//nested
+for(let i = 0; i < md3.nestedTags.length; i++) {
+    //open
+    HTMLstr += `<${md3.nestedTags[i].tagName}`
+    //attrs
+    if(md3.nestedTags[i].attrs.length !== 0) {
+        for(let attr in md3.nestedTags[i].attrs) {
+            HTMLstr += ` ${attr}="${md3.nestedTags[i].attrs[attr]}" `
+        }
+    }
+    HTMLstr += '>'
+    //nested
+    for(let nested = 0; nested < md3.nestedTags[i].nestedTags.length; nested++) {
+        HTMLstr += `<${md3.nestedTags[i].nestedTags[nested].tagName}`
+        //attrs
+        if(md3.nestedTags[i].nestedTags[nested].attrs.length !== 0) {
+            for(let attr in md3.nestedTags[i].nestedTags[nested].attrs) {
+                HTMLstr += ` ${attr}="${md3.nestedTags[i].nestedTags[nested].attrs[attr]}" `
+            }
+        }
+        HTMLstr += '>'
+
+        HTMLstr += `${md3.nestedTags[i].nestedTags[nested].text}`
+
+        HTMLstr += `</${md3.nestedTags[i].nestedTags[nested].tagName}>`
+    }
+    //close
+    HTMLstr += `</${md3.nestedTags[i].tagName}>`
+}
+
+HTMLstr += `</${md3.tagName}>`
+console.log('html str:',HTMLstr)
+document.write(HTMLstr)
+
+//destruct array
+let arr = [1,2,3,4,5, "a", "b", "c"]
+
+let [even1, even2] = arr.filter(item => isNaN(item)? false: !(item % 2))
+let [odd1, odd2, odd3] = arr.filter(item => isNaN(item)? false: item % 2)
+let letters = arr.filter(item => isNaN(item))
+
+console.log(even1, even2)
+console.log(odd1, odd2, odd3)
+console.log(letters)
+
+let arr2 = [1, 'abc']
+
+let [number=[0], [s1, s2, s3]] = arr2
+console.log(number, s1, s2, s3)
+
+//destruct 2
+let obj = {
+    name: 'Ivan',
+    surname: 'Petrov',
+    children: [{name: 'Maria'}, {name: 'Nikolay'}]
+}
+
+let {children:[{name:name1}, {name:name2}]} = obj
+console.log(name1, name2)
+
+//destruct 3
+let destruct3 = [1,2,3,4, 5,6,7,10]
+// A и B ВМЕСТО МАЛЕНЬКИХ a и b поскольку я их уже использовал в коде
+let {0:A, 1:B, length} = destruct3
+console.log(A, B, length)
+
+//black belt
+let history = '1111'
+let predictArray = {}
+
+while(true) {
+    console.log(`My prediction is: ${predictArray[history]? predictArray[history] : Math.random() > 0.5? 1 : 0}`)
+    let newValue = prompt('input 1 or 2', '')
+
+    if(+newValue === 1 || +newValue === 2) {
+        predictArray[history] = +newValue
+
+        history = history.split('')
+        history.shift()
+        history.push(newValue)
+        history = history.join('')
+    } else if(newValue === null){
+        break;
+    } else {
+        alert('wrong value given')
+    }
+}