123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273 |
- //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 = "<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>"
- document.write(str)
- //Html optional fields
- let htmlOptional = '<h2>HTML optional fields</h2><table border="1">'
- let tableHeaders = Object.keys(persons[0])
- htmlOptional += '<tr style="background-color: black; color: white;">'
- for(let i = 0; i < tableHeaders.length; i++) {
- for(let j in persons[i]) {
- if(!tableHeaders.includes(j)) {
- tableHeaders.push(j)
- }
- }
- htmlOptional += `<th>${tableHeaders[i]}</th>`
- }
- htmlOptional += '</tr>'
- for(let i = 0; i < persons.length; i++) {
- //HTML tr color
- htmlOptional += i % 2 === 0? '<tr style="background-color: firebrick; color: white">' : '<tr>'
- //HTML th optional
- for(let cols = 0; cols < tableHeaders.length; cols++) {
- htmlOptional += persons[i][tableHeaders[cols]]? `<td style="text-align:center">${persons[i][tableHeaders[cols]]}</td>` :
- `<td style="text-align:center; color: black;">X</td>`
- }
- htmlOptional += '</tr>'
- }
- htmlOptional += '</table>'
- 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')
- }
- }
|