//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])
}
}
fieldCheck(a, 'age')
fieldCheck(b, 'awoken')
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 = "
"
for (let i = 0; i < persons.length; i++) {
str += `${persons[i].Name} | ${persons[i].surname} |
`
}
str += "
"
document.write(str)
//Html optional fields
let htmlOptional = 'HTML optional fields
'
let tableHeaders = Object.keys(persons[0])
htmlOptional += ''
for(let i = 0; i < tableHeaders.length; i++) {
for(let j in persons[i]) {
if(!tableHeaders.includes(j)) {
tableHeaders.push(j)
}
}
htmlOptional += `${tableHeaders[i]} | `
}
htmlOptional += '
'
for(let i = 0; i < persons.length; i++) {
//HTML tr color
htmlOptional += i % 2 === 0? '' : '
'
//HTML th optional
for(let cols = 0; cols < tableHeaders.length; cols++) {
htmlOptional += persons[i][tableHeaders[cols]]? `${persons[i][tableHeaders[cols]]} | ` :
`X | `
}
htmlOptional += '
'
}
htmlOptional += '
'
document.write(htmlOptional)
// blue belt
class Markup {
constructor(tagName, [...nestedTags]=[], {...attrs}={}, text='') {
this.tagName = tagName,
this.nestedTags = nestedTags
this.attrs = attrs
this.text = text
}
}
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 = `Blue belt
<${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')
}
}