//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.attrs = attrs
this.text = text
this.nestedTags = nestedTags
}
}
let md3 = new Markup(
'table',
[
new Markup(
'tr',
[
new Markup(
'td',
undefined,
{disabled:true},
'some text'
),
new Markup(
'td',
undefined,
undefined,
'some text2'
)
]
),
new Markup(
'tr',
[
new Markup(
'td',
[
new Markup(
'div',
[
new Markup(
'p',
undefined,
{disabled:true},
'some textp'
),
new Markup(
'p',
undefined,
{style:'font-weight:800;color:red'},
'some textp'
)
]
)
],
{style:'background-color:purple;color:white;'},
'some text div'
),
new Markup(
'td',
undefined,
{class:'someclass'},
'some text4'
)
]
)
],
{border: 1, style:"color:'red';"}
)
function stringifyMdObject(Md_obj) {
let str = ''
for(let key in Md_obj) {
if(key === 'tagName') {
str += `<${Md_obj[key]}`
if(Md_obj.attrs.length !== 0) {
continue;
} else {
str += `>`
}
}
if(key === 'nestedTags') {
for(let i = 0; i < Md_obj[key].length; i++) {
str += stringifyMdObject(Md_obj[key][i])
}
}
if(key === 'attrs' && Md_obj[key].length !== 0) {
for(let i in Md_obj.attrs) {
str += ` ${i}="${Md_obj.attrs[i]}"`
}
str += `>`
}
}
str += `${Md_obj.text}`
str += `${Md_obj.tagName}>`
return str
}
document.write(stringifyMdObject(new Markup('h2',[new Markup('span',undefined,{style:'color:blue;'},'Blue ')],undefined,'belt')))
document.write(stringifyMdObject(md3))
//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')
}
}