script.js 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  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.attrs = attrs
  107. this.text = text
  108. this.nestedTags = nestedTags
  109. }
  110. }
  111. let md3 = new Markup(
  112. 'table',
  113. [
  114. new Markup(
  115. 'tr',
  116. [
  117. new Markup(
  118. 'td',
  119. undefined,
  120. {disabled:true},
  121. 'some text'
  122. ),
  123. new Markup(
  124. 'td',
  125. undefined,
  126. undefined,
  127. 'some text2'
  128. )
  129. ]
  130. ),
  131. new Markup(
  132. 'tr',
  133. [
  134. new Markup(
  135. 'td',
  136. [
  137. new Markup(
  138. 'div',
  139. [
  140. new Markup(
  141. 'p',
  142. undefined,
  143. {disabled:true},
  144. 'some textp'
  145. ),
  146. new Markup(
  147. 'p',
  148. undefined,
  149. {style:'font-weight:800;color:red'},
  150. 'some textp'
  151. )
  152. ]
  153. )
  154. ],
  155. {style:'background-color:purple;color:white;'},
  156. 'some text div'
  157. ),
  158. new Markup(
  159. 'td',
  160. undefined,
  161. {class:'someclass'},
  162. 'some text4'
  163. )
  164. ]
  165. )
  166. ],
  167. {border: 1, style:"color:'red';"}
  168. )
  169. function stringifyMdObject(Md_obj) {
  170. let str = ''
  171. for(let key in Md_obj) {
  172. if(key === 'tagName') {
  173. str += `<${Md_obj[key]}`
  174. if(Md_obj.attrs.length !== 0) {
  175. continue;
  176. } else {
  177. str += `>`
  178. }
  179. }
  180. if(key === 'nestedTags') {
  181. for(let i = 0; i < Md_obj[key].length; i++) {
  182. str += stringifyMdObject(Md_obj[key][i])
  183. }
  184. }
  185. if(key === 'attrs' && Md_obj[key].length !== 0) {
  186. for(let i in Md_obj.attrs) {
  187. str += ` ${i}="${Md_obj.attrs[i]}"`
  188. }
  189. str += `>`
  190. }
  191. }
  192. str += `${Md_obj.text}`
  193. str += `</${Md_obj.tagName}>`
  194. return str
  195. }
  196. document.write(stringifyMdObject(new Markup('h2',[new Markup('span',undefined,{style:'color:blue;'},'Blue ')],undefined,'belt')))
  197. document.write(stringifyMdObject(md3))
  198. //destruct array
  199. let arr = [1,2,3,4,5, "a", "b", "c"]
  200. let [even1, even2] = arr.filter(item => isNaN(item)? false: !(item % 2))
  201. let [odd1, odd2, odd3] = arr.filter(item => isNaN(item)? false: item % 2)
  202. let letters = arr.filter(item => isNaN(item))
  203. console.log(even1, even2)
  204. console.log(odd1, odd2, odd3)
  205. console.log(letters)
  206. let arr2 = [1, 'abc']
  207. let [number=[0], [s1, s2, s3]] = arr2
  208. console.log(number, s1, s2, s3)
  209. //destruct 2
  210. let obj = {
  211. name: 'Ivan',
  212. surname: 'Petrov',
  213. children: [{name: 'Maria'}, {name: 'Nikolay'}]
  214. }
  215. let {children:[{name:name1}, {name:name2}]} = obj
  216. console.log(name1, name2)
  217. //destruct 3
  218. let destruct3 = [1,2,3,4, 5,6,7,10]
  219. // A и B ВМЕСТО МАЛЕНЬКИХ a и b поскольку я их уже использовал в коде
  220. let {0:A, 1:B, length} = destruct3
  221. console.log(A, B, length)
  222. //black belt
  223. let history = '1111'
  224. let predictArray = {}
  225. while(true) {
  226. console.log(`My prediction is: ${predictArray[history]? predictArray[history] : Math.random() > 0.5? 1 : 0}`)
  227. let newValue = prompt('input 1 or 2', '')
  228. if(+newValue === 1 || +newValue === 2) {
  229. predictArray[history] = +newValue
  230. history = history.split('')
  231. history.shift()
  232. history.push(newValue)
  233. history = history.join('')
  234. } else if(newValue === null){
  235. break;
  236. } else {
  237. alert('wrong value given')
  238. }
  239. }