script.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. //3 persons
  2. let a = {
  3. 'Name': 'Sergei',
  4. 'surname': 'Levshnia',
  5. 'fathername': 'Sergeevich'
  6. }
  7. console.log(a['Name'], a.surname)
  8. let b = {
  9. 'Name': 'Thomas',
  10. surname: 'Anderson'
  11. }
  12. console.log(b.Name, b.surname)
  13. let c = {
  14. Name: 'Sylvester',
  15. surname: 'Stallone'
  16. }
  17. console.log(c['Name'], c['surname'])
  18. //different fields
  19. a.age = 20
  20. b.nickname = 'Neo'
  21. c.movies = ['Rocky', 'Rambo: First Blood', 'Cobra', 'Over the Top']
  22. console.log(a, b, c)
  23. //fields Check
  24. function fieldCheck(obj, string) {
  25. if(string in obj) {
  26. alert(obj[string])
  27. }
  28. }
  29. fieldCheck(a, 'age')
  30. fieldCheck(b, 'awoken')
  31. fieldCheck(c, 'movies')
  32. //array of persons
  33. let persons = []
  34. persons.push(a, b, c)
  35. persons[persons.length] = {
  36. Name: 'Billy',
  37. surname: 'Joebob'
  38. }
  39. //loop of persons
  40. console.log('------------------------')
  41. console.log('loop of persons:')
  42. for(i in persons) {
  43. console.log(persons[i])
  44. }
  45. //loop of name and surname
  46. console.log('------------------------')
  47. console.log('loop of name and surname:')
  48. for(i in persons) {
  49. console.log(persons[i].Name, persons[i].surname)
  50. }
  51. //loop of loop of values
  52. console.log('------------------------')
  53. console.log('loop of loop of values:')
  54. for(let i in persons) {
  55. for(let j in persons[i]) {
  56. console.log(persons[i][j])
  57. }
  58. console.log('!-----------------------')
  59. }
  60. //fullName
  61. for(let i in persons) {
  62. persons[i].fullName = `${persons[i].surname} ${persons[i].Name}` + (persons[i].fathername? ` ${persons[i].fathername}` : '')
  63. }
  64. //serialize
  65. let serialize = JSON.stringify(persons)
  66. console.log(serialize)
  67. //deserialize
  68. persons.push(JSON.parse(serialize)[0])
  69. console.log(persons)
  70. //HTML
  71. let str = "<table border='1'>"
  72. for (let i = 0; i < persons.length; i++) {
  73. str += `<tr><td>${persons[i].Name}</td><td>${persons[i].surname}</td></tr>`
  74. }
  75. str += "</table>"
  76. document.write(str)
  77. //Html optional fields
  78. let htmlOptional = '<h2>HTML optional fields</h2><table border="1">'
  79. let tableHeaders = Object.keys(persons[0])
  80. htmlOptional += '<tr style="background-color: black; color: white;">'
  81. for(let i = 0; i < tableHeaders.length; i++) {
  82. for(let j in persons[i]) {
  83. if(!tableHeaders.includes(j)) {
  84. tableHeaders.push(j)
  85. }
  86. }
  87. htmlOptional += `<th>${tableHeaders[i]}</th>`
  88. }
  89. htmlOptional += '</tr>'
  90. for(let i = 0; i < persons.length; i++) {
  91. //HTML tr color
  92. htmlOptional += i % 2 === 0? '<tr style="background-color: firebrick; color: white">' : '<tr>'
  93. //HTML th optional
  94. for(let cols = 0; cols < tableHeaders.length; cols++) {
  95. htmlOptional += persons[i][tableHeaders[cols]]? `<td style="text-align:center">${persons[i][tableHeaders[cols]]}</td>` :
  96. `<td style="text-align:center; color: black;">X</td>`
  97. }
  98. htmlOptional += '</tr>'
  99. }
  100. htmlOptional += '</table>'
  101. document.write(htmlOptional)
  102. // blue belt
  103. class Markup {
  104. constructor(tagName, [...nestedTags]=[], {...attrs}={}, text='') {
  105. this.tagName = tagName,
  106. this.nestedTags = nestedTags
  107. this.attrs = attrs
  108. this.text = text
  109. }
  110. }
  111. let md3 = new Markup('table', [new Markup('tr', [new Markup('td', undefined, undefined, 'some text'),new Markup('td', undefined, undefined, 'some text2')])],{border: 1})
  112. console.dir(md3)
  113. let HTMLstr = `<h2>Blue belt</h2><${md3.tagName}`
  114. if(md3.attrs.length !== 0) {
  115. for(let i in md3.attrs) {
  116. HTMLstr += ` ${i}="${md3.attrs[i]}" `
  117. }
  118. }
  119. HTMLstr += '>'
  120. //nested
  121. for(let i = 0; i < md3.nestedTags.length; i++) {
  122. //open
  123. HTMLstr += `<${md3.nestedTags[i].tagName}`
  124. //attrs
  125. if(md3.nestedTags[i].attrs.length !== 0) {
  126. for(let attr in md3.nestedTags[i].attrs) {
  127. HTMLstr += ` ${attr}="${md3.nestedTags[i].attrs[attr]}" `
  128. }
  129. }
  130. HTMLstr += '>'
  131. //nested
  132. for(let nested = 0; nested < md3.nestedTags[i].nestedTags.length; nested++) {
  133. HTMLstr += `<${md3.nestedTags[i].nestedTags[nested].tagName}`
  134. //attrs
  135. if(md3.nestedTags[i].nestedTags[nested].attrs.length !== 0) {
  136. for(let attr in md3.nestedTags[i].nestedTags[nested].attrs) {
  137. HTMLstr += ` ${attr}="${md3.nestedTags[i].nestedTags[nested].attrs[attr]}" `
  138. }
  139. }
  140. HTMLstr += '>'
  141. HTMLstr += `${md3.nestedTags[i].nestedTags[nested].text}`
  142. HTMLstr += `</${md3.nestedTags[i].nestedTags[nested].tagName}>`
  143. }
  144. //close
  145. HTMLstr += `</${md3.nestedTags[i].tagName}>`
  146. }
  147. HTMLstr += `</${md3.tagName}>`
  148. console.log('html str:',HTMLstr)
  149. document.write(HTMLstr)
  150. //destruct array
  151. let arr = [1,2,3,4,5, "a", "b", "c"]
  152. let [even1, even2] = arr.filter(item => isNaN(item)? false: !(item % 2))
  153. let [odd1, odd2, odd3] = arr.filter(item => isNaN(item)? false: item % 2)
  154. let letters = arr.filter(item => isNaN(item))
  155. console.log(even1, even2)
  156. console.log(odd1, odd2, odd3)
  157. console.log(letters)
  158. let arr2 = [1, 'abc']
  159. let [number=[0], [s1, s2, s3]] = arr2
  160. console.log(number, s1, s2, s3)
  161. //destruct 2
  162. let obj = {
  163. name: 'Ivan',
  164. surname: 'Petrov',
  165. children: [{name: 'Maria'}, {name: 'Nikolay'}]
  166. }
  167. let {children:[{name:name1}, {name:name2}]} = obj
  168. console.log(name1, name2)
  169. //destruct 3
  170. let destruct3 = [1,2,3,4, 5,6,7,10]
  171. // A и B ВМЕСТО МАЛЕНЬКИХ a и b поскольку я их уже использовал в коде
  172. let {0:A, 1:B, length} = destruct3
  173. console.log(A, B, length)
  174. //black belt
  175. let history = '1111'
  176. let predictArray = {}
  177. while(true) {
  178. console.log(`My prediction is: ${predictArray[history]? predictArray[history] : Math.random() > 0.5? 1 : 0}`)
  179. let newValue = prompt('input 1 or 2', '')
  180. if(+newValue === 1 || +newValue === 2) {
  181. predictArray[history] = +newValue
  182. history = history.split('')
  183. history.shift()
  184. history.push(newValue)
  185. history = history.join('')
  186. } else if(newValue === null){
  187. break;
  188. } else {
  189. alert('wrong value given')
  190. }
  191. }